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