Web Hosting Talk







View Full Version : Cron Job


cfaice
10-22-2005, 04:14 AM
Hi all,
I have a few cron jobs setup at the moment, but would like to have just one that will read from a txt file to process the commands.

I have written the following:

rm -f /home/planet/public_html/backup/Webmail.sql
rm -f /home/planet/public_html/backup/Forum.sql
rm -f /home/planet/public_html/backup/Site.tar.gz

mysqldump -h localhost -u example --password=example --opt planet_webmail > /home/planet/public_html/backup/Webmail.sql
mysqldump -h localhost -u example --password=example --opt planet_forums > /home/planet/public_html/backup/Forum.sql

tar cvf /home/planet/public_html/backup/Site.tar public_html
gzip /home/planet/public_html/backup/Site.tar

Will the above work?

PlanetWebHost
10-22-2005, 06:25 AM
you should write a shell script, and have your crontab run that.


myself, I'd write in PHP, just because I'm more comfortable in php than I am in BASH scripting.

crontab.php:
<?
`rm -f /home/planet/public_html/backup/Webmail.sql`
`rm -f /home/planet/public_html/backup/Forum.sql`
`rm -f /home/planet/public_html/backup/Site.tar.gz`

`mysqldump -h localhost -u example --password=example --opt`
`planet_webmail > /home/planet/public_html/backup/Webmail.sql`
`mysqldump -h localhost -u example --password=example --opt`
`planet_forums > /home/planet/public_html/backup/Forum.sql`

`tar cvf /home/planet/public_html/backup/Site.tar public_html`
`gzip /home/planet/public_html/backup/Site.tar`
?>
`

cfaice
10-22-2005, 06:32 AM
Thanks for the reply.

With the PHP file, would i still be able to run that by entering the details into the cron job section within cPanel?

innova
10-24-2005, 04:07 PM
It would be much better to do in shell scripting rather than php. That way you can at least check the return codes and take action on error or failure.

At a minimum there you would probably check for existance of files, and check return codes ($?) after each statement to ensure that everything worked as needed.

glued2
10-24-2005, 05:17 PM
The php script above just escapes everything and passes it to bash, so there isn't much need for php.
----
#!/bin/bash
rm -f /home/planet/public_html/backup/Webmail.sql
rm -f /home/planet/public_html/backup/Forum.sql
rm -f /home/planet/public_html/backup/Site.tar.gz

mysqldump -h localhost -u example --password=example --opt \
planet_webmail > /home/planet/public_html/backup/Webmail.sql
mysqldump -h localhost -u example --password=example --opt \
planet_forums > /home/planet/public_html/backup/Forum.sql

tar cvf /home/planet/public_html/backup/Site.tar public_html
gzip /home/planet/public_html/backup/Site.tar
----

You may have to put the full path of the commands in for cron to be able to access them. For example rm on my server is found in /bin/rm so above you may need /bin/rm rather than just rm. (You can use whereis rm to find it).