docker exec is not working in cron

for what it's worth I had this exact same problem. Fixing your PATH, changing permissions, and making sure you are running as the appropriate docker user are all good things, but that's not enough. It's going to continue failing because you are using "docker exec -it", which tells docker to use an interactive shell. Change it to "docker exec -t" and it'll work fine. There will be no log output anywhere telling you this, however. Enjoy!


Your docker exec command says it needs "pseudo terminal and runs in interactive mode" (-it flags) while cron doesn't attach to any TTYs.

Try changing your docker exec command to this and see if that works?

docker exec mongodb mongodump -d meteor -o /dump/

cron debugging

1. /var/log or sendmail

As crond work as a daemon, without ability of failing, execution is more important than logging. Then by default, if something goes wrong, cron will send a mail to $USER@localhost reporting script output and errors.

Have a look at /var/mail or /var/spool/mail for some mails, maybe

and at /etc/aliases to see where root's mail are sents.

2. crond and $PATH

When you run a command by cron, have care that $PATH is user's default path and not root default path (ie no */sbin and other reserved path to super user tools).

For this, the simplier way is to print your default path in the environment where everything run fine:

echo $PATH

or patch your script from command line:

sed -e "2aPATH='$PATH'" -i /root/scripts/direct.sh

This will add current $PATH initializer at line 2 in your script.

Or this, will whipe from your script all other PATH=:

sed -e "s/PATH=[^ ]*\( \|$\)/\1/;2aPATH='$PATH'" -i /root/scripts/direct.sh

3. Force logging

Add at top of your script:

exec 1>/tmp/cronlog-$$.log
exec 2>/tmp/cronlog-$$.err

Try this:

sed -e '1a\\nexec 1>/tmp/cronlog-$$.log\nexec 2>/tmp/cronlog-$$.err' -i ~/scripts/direct.sh

Finalized script could look like:

#!/bin/bash

# uncomment two following lines to force log to /tmp
# exec 1>/tmp/cronlog-$$.log
# exec 2>/tmp/cronlog-$$.err

PATH='....' # copied from terminal console!

docker exec -it mongodb mongodump -d meteor -o /dump/

Executable flag

If you run your script by

40 05 * * * bash /root/scripts/direct.sh

no executable flag are required, but you must add them:

chmod +x ~/scripts/direct.sh

if you want to run:

40 05 * * * /root/scripts/direct.sh