I have pretty simple command which is working fine standalone as a command or bash script but not when I put it in crontab
40 05 * * * bash /root/scripts/direct.sh >> /root/cron.log
which has following line
PATH=$PATH:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
SHELL=/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/root/
# Mongo Backup
docker exec -it mongodb mongodump -d meteor -o /dump/
I tried to change the url of script to /usr/bin/scirpts/
no luck
I even tried to run script directly in cron
26 08 * * * docker exec -it mongodb mongodump -d meteor -o /dump/ >> /root/cron.log
with no luck, any help appreciated.
EDIT
I don't see any errors in /root/cron.log
file either
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!
Are you sure your script is running? Add an other command like
touch /tmp/cronok
before thedocker exec
call.Don't forget that the crontab needs a newline at the end. Use
crontab -e
to edit it.Restart the cron service and check the logs (
grep -i cron /var/log/syslog
).If your OS is redhat/centos/fedora, you should try with the username (
root
) between the frequency and the command.Check your mails with the
mail
command.Check the crontab permissions.
chmod 644 /etc/crontab
.Maybe you just don't want to reinvent the wheel.
cron debugging
1.
/var/log
orsendmail
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, maybeand 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:
or patch your script from command line:
This will add current
$PATH
initializer at line 2 in your script.Or this, will whipe from your script all other
PATH=
:3. Force logging
Add at top of your script:
Try this:
Finalized script could look like:
Executable flag
If you run your script by
no executable flag are required, but you must add them:
if you want to run:
1) Make sure this task is in the root user's crontab - it's probably the case but you didn't write it explicitly
2)
cron
may be unable to findbash
. I would remove it and call directly your script after making it executable:and then set your crontab entry as
40 05 * * * /root/scripts/direct.sh 2>&1 >> /root/cron.log
If it's still not working, then you should have some useful output in
/root/cron.log
Here's a few things I'd change-- first, capture STDERR along with STDOUT and remove the shell specification in cron-- use #! in your script instead.
40 05 * * * /root/scripts/direct.sh &>> /root/cron.log
Next, you are setting your PATH in the reverse order, and you are missing your shbang. I have no idea why you are defining SHELL as /bin/sh, when you are running bash, instead of dash. Change your script to this.
See if that yields something better to work with.
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?