Help with a simple CRON job?

Status
Not open for further replies.

xweb3x10

Member
Messages
34
Reaction score
1
Points
8
I have created CRON jobs at other hosts but I am really struggling here. I have tried reading through all of the posts related to CRON jobs but I'm not getting anywhere. Any help would be greatly appreciated.

Once a day I'd like to...
  • Recursively delete contents of public_html/jobs directory.
  • Copy contents of public_html/newjobs to public_html/jobs directory.
  • Update sql tables with newjobs.sql
  • Send me an email saying whether it was completed or if there was an error. I can use offsite smtp settings, if necessary.
Is this possible?
 
Last edited:

xweb3x10

Member
Messages
34
Reaction score
1
Points
8
Okay. after many hours of trying to nail this down I've been able to create three con jobs:
  1. Calls a php file that emails me.
  2. Recursively deletes my target directory, including hidden files.
  3. Copies my source directory satisfactorily.
I get to see any error messages by adding
>>/home/user/public_html/cronlog.txt 2>&1
to the end. Cron jobs I still need to create:
  1. Drop all db tables.
  2. Import local copy of backup sql file.
I created a sql file that, when imported via phpmyadmin, restores the db's desired tables. But it won't delete any additional tables that may have been created. And I still need help writing a cron job to do it.

Ideally, I'd like all of the cron jobs to be in one script so that they can be set to run consecutively. That way, there'd be no chance of copying a file that has yet to be deleted.

I've tried getting the delete and copy jobs on the same line using && but only one of the jobs will complete and there is no error listed in the log file.

Thanks again for any suggestions of help.
 

xweb3x10

Member
Messages
34
Reaction score
1
Points
8
Ok. I got this done. To review, I offer a demo site for users be able to test drive the Joomla! CMS software platform. I want to reset the site every hour so that any edits people have made will be undone.

To completely delete the Joomla! demo directory your cron command is:
rm -r -f /home/xweb3x10/public_html/_DEMO/JOOMLAD/
To copy an archive directory to the demo directory your cron command is:
cp -r /home/xweb3x10/public_html/_DEMO/ARCHIVE/JOOMLAD /home/xweb3x10/public_html/_DEMO
To delete the database, create the database and import the data you'll need to create a file named /home/xweb3x10/scripts/restore_data.sh containing the following code (adjusted for your db credentials)
#! /bin/sh
TERM=dumb
export TERM
clear;

#here the script deletes all your tables in the database

mysql -u xweb3x10_demos -ppassword -e 'drop database xweb3x10_demos;'

mysql -u xweb3x10_demos -ppassword -e 'create database xweb3x10_demos;'

#here the script restores the database

mysql -u xweb3x10_demos -ppassword xweb3x10_demos < /home/xweb3x10/backups/demo.sql

Then, via phpmyadmin, export your database to home/xweb3x10/backups/demo.sql
Once done, your cron command is:
/home/xweb3x10/scripts/restore_data.sh
I also wanted to create a log for any errors. You can do this by adding the following code to any cron command:
>>/home/xweb3x10/public_html/cronlog.txt 2>&1
For example:
rm -r -f /home/xweb3x10/public_html/_DEMO/JOOMLAD/ >>/home/xweb3x10/public_html/cronlog.txt 2>&1
I needed to make sure all of these cron commands ran as quickly as possible and consecutively (not individually) . This is important because I don't want to be copying a file or folder that has not yet been deleted. To do this I concatenated them using &&. Here is the result:
0 * * * * rm -r -f /home/xweb3x10/public_html/_DEMO/JOOMLAD/ && cp -r /home/xweb3x10/public_html/_DEMO/ARCHIVE/JOOMLAD /home/xweb3x10/public_html/_DEMO && /home/xweb3x10/scripts/restore_data.sh && >>/home/xweb3x10/public_html/cronlog.txt 2>&1
Note: To verify that this all works I created a directory in the demo folder via ftp and a new table via phpmyadmin. Once the command completed the routine I checked for the folder and the table to ensure they were deleted. The emailing part was no longer necessary for me once I eliminated all errors and the command performed as desired.
 

xweb3x10

Member
Messages
34
Reaction score
1
Points
8
Update: In case anyone is wondering what the process is to edit the site here are the steps:
  1. Make copies of the old demo.sql and the old "copy" directory.
  2. Start this process a few minutes after the hour. Set the Cron job to run once a year.
  3. Make the edits that you want, ensuring that they are completed before the New Year.
  4. If your edits were only related to text or settings then move on to step 6.
  5. Copy all the files to your "copy" directory (only necessary if you altered the directory structure).
  6. Export all tables to a file and rename it to "demo.sql". Upload it to the "/backups" directory.
  7. Set the Cron job back to your preferred schedule.
Check your work. Make a small edit to the site and verify that the site resets at the top of the hour.
 
Status
Not open for further replies.
Top