Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a shell script to get a MySQL database dump. This script works fine when run manually

fqn=/home/Mysqluser/daily_dumps/bookstore_`date +%Y%m%d_%H%M%S`.sql.gz
mysqldump -u root -h localhost -pmysql#passwd bookstore | gzip > $fqn

But when I include this script to be run in cron job, it doesnt make the backup.

This is what I included in the cron job:

00 00 * * *  /home/Mysqluser/daily_dumps/bin/

What am I missing here?

share|improve this question

It's probably because the environment(s) (type env at prompt to see it) are different.

I had similar problem trying to run a mysql query in cron.daily, I solved it by adding this flag --defaults-file=/root/.my.cnf

It look like mysqldump has a similar flag --defaults-extra-file (needs to be first argument) but if that doesn't work try to to find out what is the difference between your command-line environment and cron environment. Create a cronjob that prints the output of env to a file and compare it to what you get when you run it from your shell.

share|improve this answer
I had something similar on running a php script through cron. The cron job was set to run as my user account, but when we delved further, the php being invoked under cron was from a completely different directory and therefore not running under the same php.ini as my interactive user account. – Fiasco Labs Feb 29 '12 at 8:17

Also, be sure that mysqldump is in the $PATH when running from cron. A best practice is to set $PATH directly in your script, or to specify full paths to any commands that are invoked (e.g., /usr/bin/mysqldump).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.