Score:2

Script in cron.hourly does not run

us flag

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!

us flag
1) If `shellcheck` does not display anything, it means that the script is syntactically correct. 2) Please never use `chmod 777` to make scripts world-writable (`755` is enough, `u+rx` is minimum requirement). 3) The part regarding daily/weekly/monthly tasks can be stripped, it's unrelated (and anacron is _not_ needed here; the test says "execute the right-hand command only if anacron is not found and executable, in which case the latter takes care of this!) 4) Are you sure that your problem persists?
hr flag
btw if you ever uncomment `#if [[` you will need to switch the shebang back to `#!/bin/bash` since POSIX sh doesn't support the ksh-style `[[...]]` extended test
geekygeek avatar
us flag
Thank you very much for the tips. I will keep that in mind and also apply what you mentioned to my script
geekygeek avatar
us flag
Okay, I have edited my answer to include what is now being displayed in the logs.
geekygeek avatar
us flag
Sure will do, thanks.
Score:1
us flag

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.

hr flag
Remember you can accept your own answer ;)
Score:0
us flag

Line four of your script looks suspicious, as confirmed by shellcheck:

% shellcheck rclone_Linux_MongoDB_Sync
In rclone_Linux_MongoDB_Sync line 4:
/usr/bin/docker exec -it mongodb mongodump --db dashboarddb -u hifi_dbpc_host ->
    ^-- SC1009: The mentioned syntax error was in this simple command.
    ^-- SC1073: Couldn't parse this redirection. Fix to allow more checks.
    ^-- SC1072: Fix any mentioned problems and try again.
geekygeek avatar
us flag
Apologies. It appears that while copy and pasting, there were some errors. The -> I believe was from cat, indicating that the line continues. I have now updated the code.
geekygeek avatar
us flag
When I do run shellscript locally, there is nothing displayed
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.