PDA

View Full Version : A problem with scripts for crontab doesn't run as expected




Kann
Jan 27, 2011, 11:07 PM
Dear all,

I am trying to write a back up scripts for all database at my office; so, I wrote a mysqldump script and trying to use crontab to schedule run the daily backup. However, the backup sql file is generated, but with an empty content. It seems that the cron daemon does run the job, but return an empty file. I tried running the script on command line and it worked just fine (the proper backup script was returned). Why does my backup script work with out problem when I execute it manually from command line but not work from crontab/cron daemon?

All backup scripts and the crontab file were written by root account. Below is my short back up script and the crontab file:

sh-3.2# crontab -l
55 11 * * * root /bin/sh /Volumes/Pharmakon/bin/backup/database.sh
sh-3.2#



sh-3.2# more /Volumes/Pharmakon/bin/backup/database.sh
/usr/bin/mysqldump -u root --password=xxx --all-databases >/Volumes/Pharmakon/backup/databases/backup.sql
sh-3.2#



ChrisA
Jan 28, 2011, 11:44 AM
The dump command runs as root. Do you actually run the database as root with all file owned by root? Most people would make a special user just for the databse. If you are setup like that then perhaps you should run the dump as that user.

I don't use mysql but other DBMSes will refuse to run under root as that is to dangerous

Kann
Jan 28, 2011, 12:19 PM
The dump command runs as root. Do you actually run the database as root with all file owned by root? Most people would make a special user just for the databse. If you are setup like that then perhaps you should run the dump as that user.

I don't use mysql but other DBMSes will refuse to run under root as that is to dangerous

I don't intend to run the dump command as a root, but was kinda using the root account while I was testing all the backup commands to make sure things don't get the permission issue.

The weird thing is that the script worked just find from command line, but did nothing from the cronjob.

belvdr
Jan 28, 2011, 03:26 PM
I don't intend to run the dump command as a root, but was kinda using the root account while I was testing all the backup commands to make sure things don't get the permission issue.

The weird thing is that the script worked just find from command line, but did nothing from the cronjob.

Add 2>&1 to the end of your command in cron and check the log.

The main difference here is that running from a shell, you have a totally different environment versus what cron runs from.

Cromulent
Jan 28, 2011, 05:34 PM
Also it should be noted that launchd is preferred over cron on Mac OS X.

nopster
Nov 4, 2011, 06:37 PM
Hi, i have a similar issue.

I have a jar file that launches mysqldump queries on several dbs and servers.

If I launch from the command line the batch mysqldump dumps every db in the list.

I tried Cronnix to make the backups every 24 hours, but the mysqldump reports 0 byte of every single dump of dbs.

Then I tried Lingon to configure the same batch and making a launchd. But even here the dump are 0 byte.

Previously on Mac OS X Server 10.6 everything worked fine, now that this procedure runs on Lion client with Java runtime installed work nomore.

Every type of help is really appreciated.

Thanks in advance.