Server Fault is a question and answer site for system and network administrators. 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

This is my script,

suffix=$(date +%w)
rm /home/myuser/backup/mongo-$suffix -rf
mkdir /home/myuser/backup/mongo-$suffix
mongodump -h$1 -u$2 -p$3 -dmydb -o/home/myuser/backup/mongo-$suffix

In my crontab -e:

0   3  *   *   *    /bin/bash  /home/myuser/myproject/production/  localhost user1 pass1

When I run the script normally (executing the command above), the script works and the directory "mydb" gets created in /mongo-$suffix.

However, when I put this in crontab, /mongo-$suffix gets created but the "mydb" directory does not exist. There's nothing in /mongo-$suffix.

share|improve this question
up vote 3 down vote accepted

You may not have mongodump in your PATH. Try using the absolute path to the mongodump command.

share|improve this answer
If I log in as "myuser", and I just run mongodump, it works. And also, I did put it in /etc/profile. PATH=$PATH:/opt/mongodb/bin export PATH – Alex May 15 '11 at 20:25
Try doing exactly what this answer suggests, putting the full path in the script, or at least execute it as $(which mongodump). Your cron may not be loading anything from your profile. – Caleb May 15 '11 at 20:41

It is very likely a PATH issue but I would recommend to NOT override PATH in your crontab, because it is

  • really easy to avoid the behavior
  • may be a security risk

Its good bash/sh coding style to define every binary that you want to trigger with with it's full path as variable



and refer to the variable later.

$RSYNC /foo/bar baz

There are also certain options to change the user that is executing the crontab. Assuming that there is a user called mongodump that has defined a PATH that includes mongodump, you could become this user and edit it's crontab. Or you could also use

/bin/bash /bin/su - mongodump -c /bin/bash /home/myuser/myproject/production/

in roots's crontab.

share|improve this answer

The default PATH for cron is restricted. It is usually /usr/bin:/bin. If you want to run anything outside of those directories you have several options

Use full paths to the commands e.g. /path/to/mongodump/mongodump ...

Define an extended PATH in the crontab e.g.

    0 3 * * * /bin/bash  /home/myuser/myproject/production/  localhost user1 pass1

Define the PATH in the script you are running

suffix=$(date +%w)
rm /home/myuser/backup/mongo-$suffix -rf
mkdir /home/myuser/backup/mongo-$suffix
mongodump -h$1 -u$2 -p$3 -dmydb -o/home/myuser/backup/mongo-$suffix
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.