Script in cron.hourly does not run
I have the following file in /etc/cron.hourly
. The purpose of this script is to create a docker-MongoDB dump backup every hour and then use rclone to synchronize this dump file with the contents in a Google Drive folder.
Name of Script:
rclone_Linux_MongoDB_Sync
Script:
#!/bin/sh
/usr/bin/docker exec -it mongodb mkdir /data/dump
/usr/bin/docker exec -it mongodb mongodump --db myDB -u theUser -p thePassword --gzip --out /data/dump/
# RClone Config file
RCLONE_CONFIG=/home/ubuntu/.config/rclone/rclone.conf
export RCLONE_CONFIG
#if [[ "`pidof -x $(basename $0) -o %PPID`" ]]; then exit; fi
/usr/bin/rclone sync /data/dump/myDB/MongoDB_Backup:MongoDB_Current
I have done:
sudo chmod +x /etc/cron.hourly/rclone_Linux_MongoDB_Sync
sudo chmod 777 /etc/cron.hourly/rclone_Linux_MongoDB_Sync
Notice how I do not have the .sh
extension for the script. Previously, this was my issue. Now, when I run the following, my script does display.
run-parts --test /etc/cron.hourly
My script also runs correctly when I do:
./rclone_Linux_MongoDB_Sync
It also runs when I do:
run-parts /etc/cron.hourly
I have checked /etc/crontab with:
cat /etc/crontab
Part of the result is the following. To my understanding, this is correct.
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#
I evaluated the logs for cron with the following:
grep CRON /var/log/syslog
For the daily, weekly, and monthly, I believe they previously were not working due to anacron not being installed. I installed anacron with:
sudo apt-get install anacron
cron.hourly runs at the 17 minute mark of the hour, as indicated by /etc/crontab
. Corresponding to this, I see the following repeated each time, with a 17 timestamp.
Jul 19 17:17:01 vps-fac5a33c CRON[1039453]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Jul 19 17:17:09 vps-fac5a33c CRON[1039452]: (root) MAIL (mailed 104 bytes of output but got status 0x004b from MTA#012)
Jul 19 18:17:01 vps-fac5a33c CRON[1045250]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Jul 19 18:17:18 vps-fac5a33c CRON[1045249]: (root) MAIL (mailed 104 bytes of output but got status 0x004b from MTA#012)
Jul 19 19:17:01 vps-fac5a33c CRON[1051174]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Jul 19 19:17:21 vps-fac5a33c CRON[1051173]: (root) MAIL (mailed 104 bytes of output but got status 0x004b from MTA#012)
Perhaps there are some things that I have done that I have forgotten about and not listed here. Nevertheless, I don't understand why this is not working. I have evaluated many different forum posts, tried their process, but doesn't work for me.
Why is my script not running every hour when it is placed in /etc/cron.hourly
?
It does not run on its own. It should run every hour.
As an additional note, my script previously had:
#!/bin/bash
However, based on some posts, I changed it to:
#!/bin/sh
When I run shellcheck
on my script, nothing is displayed, indicating that it is fine according to shellcheck
and is syntactically correct:
shellcheck rclone_Linux_MongoDB_Sync
Added the following to the top of the script after #!/bin/sh
. This is to see what errors are occurring within the script during execution.
exec 1>>/tmp/rclone_Linux_MongoDB_Sync.log 2>&1
I changed the crontab for .hourly to a recent time to not have to wait until the 17 minute mark for it to run. Due to the above addition to my script file, the following file was created:
/tmp/rclone_Linux_MongoDB_Sync.log
After running:
cat /tmp/rclone_Linux_MongoDB_Sync.log
The following was displayed.
the input device is not a TTY
the input device is not a TTY
Thanks everyone!
Okay, so it appears this is an issue with Docker
. When issuing commands in Docker
through cron
, instead of -it
only use -i
. I have now changed my Docker
commands accordingly.
Changed script:
#!/bin/sh
exec 1>>/tmp/rclone_Linux_MongoDB_Sync.log 2>&1
/usr/bin/docker exec -i mongodb mkdir /data/dump
/usr/bin/docker exec -i mongodb mongodump --db myDB -u theUser -p thePassword --gzip --out /data/dump/
# RClone Config file
RCLONE_CONFIG=/home/ubuntu/.config/rclone/rclone.conf
export RCLONE_CONFIG
#if [[ "`pidof -x $(basename $0) -o %PPID`" ]]; then exit; fi
/usr/bin/rclone sync /data/dump/myDB/MongoDB_Backup:MongoDB_Current
Now, the script is able to run as intended, and runs automatically each hour. The fix was changing the -it
in the Docker
commands to just -i
.