A problem with scripts for crontab doesn't run as expected

Discussion in 'Mac OS X Server, Xserve, and Networking' started by Kann, Jan 27, 2011.

  1. Kann macrumors newbie

    Apr 7, 2009
    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# more /Volumes/Pharmakon/bin/backup/database.sh 
    /usr/bin/mysqldump -u root --password=xxx --all-databases >/Volumes/Pharmakon/backup/databases/backup.sql
  2. ChrisA macrumors G4

    Jan 5, 2006
    Redondo Beach, California
    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
  3. Kann thread starter macrumors newbie

    Apr 7, 2009
    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.
  4. belvdr macrumors 603

    Aug 15, 2005
    No longer logging into MR
    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.
  5. Cromulent macrumors 603


    Oct 2, 2006
    The Land of Hope and Glory
    Also it should be noted that launchd is preferred over cron on Mac OS X.
  6. nopster macrumors newbie

    Jun 10, 2009
    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.

Share This Page