My OS is Ubuntu 18.04.3 LTS. I have 2 crontab jobs scheduled to run every morning at 4:00 a.m. and 4.05 a.m.
00 4 * * * sh /home/developer/script/batch_load1.sh >> /home/developer/script/log/batch_load1.log 2>&1
05 4 * * * sh /home/developer/script/batch_load2.sh >> /home/developer/script/log/batch_load2.log 2>&1
In the batch_load1.sh
and batch_load2.sh
, they will read a text file and run a python script. The reason I separate 2 sh bash scripts is that I can run them in parallel and save time, since batch_load1.sh
will take a fairly long time to finish.
batch_load1.sh
:
batchdate=$(date -d '-1 day' '+%Y%m%d')
today=$(date '+%Y%m%d')
echo $(date '+%Y-%m-%d %H:%M:%S')": [DEBUG] Start python virtual environment."
cd /home/developer/script
. venv/bin/activate
echo $(date '+%Y-%m-%d %H:%M:%S')": [DEBUG] Start python script."
while read name; do
echo $(date '+%Y-%m-%d %H:%M:%S')": [DEBUG] Processing "${name}" in python script."
python -u load.py --batchdate $batchdate --target $name >> log/py_output1_$today.log
echo $(date '+%Y-%m-%d %H:%M:%S')": [DEBUG] Finished processing "${name}"."
done < view_list1.txt
echo ""
batch_load2.sh
:
batchdate=$(date -d '-1 day' '+%Y%m%d')
today=$(date '+%Y%m%d')
echo $(date '+%Y-%m-%d %H:%M:%S')": [DEBUG] Start python virtual environment."
cd /home/developer/script
. venv/bin/activate
echo $(date '+%Y-%m-%d %H:%M:%S')": [DEBUG] Start python script."
while read name; do
echo $(date '+%Y-%m-%d %H:%M:%S')": [DEBUG] Processing "${name}" in python script."
python -u load.py --batchdate $batchdate --target $name >> log/py_output2_$today.log
echo $(date '+%Y-%m-%d %H:%M:%S')": [DEBUG] Finished processing "${name}"."
done < view_list2.txt
echo ""
However, when I check the batch_load1.log and batch_load2.log, they are not started at the scheduled time and are delayed. batch_load1 job starts at 06:17:11 a.m.
2020-03-10 06:17:11: [DEBUG] Start python virtual environment.
2020-03-10 06:17:12: [DEBUG] Start python script.
2020-03-10 06:17:12: [DEBUG] Processing table1 in python script.
2020-03-10 08:27:32: [DEBUG] Finished processing table1.
batch_load2 job starts at 06:17:32 a.m.
2020-03-10 06:17:32: [DEBUG] Start python virtual environment.
2020-03-10 06:17:33: [DEBUG] Start python script.
2020-03-10 06:17:33: [DEBUG] Processing table2 in python script.
2020-03-10 06:20:17: [DEBUG] Finished processing table2.
2020-03-10 06:20:17: [DEBUG] Processing table3 in python script.
2020-03-10 06:22:50: [DEBUG] Finished processing table3.
...
2020-03-10 06:31:25: [DEBUG] Processing table10 in python script.
2020-03-10 06:31:25: [DEBUG] Finished processing table10.
If I add another job (batch_load3.sh) in the schedule to run at every 6:30 a.m., it will run on exactly 6:30 a.m. and not delaying:
30 6 * * * sh /home/developer/script/batch_load3.sh >> /home/developer/script/log/batch_load3.log 2>&1
In the /var/log/syslog.1
, I can see that job1 and 2 ran on 06:17 a.m., while job3 ran on 06:30 a.m. as scheduled:
Mar 10 00:10:45 python1 systemd[1]: Started Session 365 of user developer.
Mar 10 00:15:16 python1 snapd[126844]: storehelpers.go:436: cannot refresh snap "core": snap has no updates available
Mar 10 00:15:16 python1 snapd[126844]: autorefresh.go:397: auto-refresh: all snaps are up-to-date
Mar 10 00:17:01 python1 CRON[5740]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Mar 10 00:28:16 python1 systemd[1]: Starting Message of the Day...
Mar 10 00:28:20 python1 50-motd-news[12378]: * Latest Kubernetes 1.18 beta is now available for your laptop, NUC, cloud
Mar 10 00:28:20 python1 50-motd-news[12378]: instance or Raspberry Pi, with automatic updates to the final GA release.
Mar 10 00:28:20 python1 50-motd-news[12378]: sudo snap install microk8s --channel=1.18/beta --classic
Mar 10 00:28:20 python1 50-motd-news[12378]: * Multipass 1.1 adds proxy support for developers behind enterprise
Mar 10 00:28:20 python1 50-motd-news[12378]: firewalls. Rapid prototyping for cloud operations just got easier.
Mar 10 00:28:20 python1 50-motd-news[12378]: https://multipass.run/
Mar 10 00:28:20 python1 systemd[1]: Started Message of the Day.
Mar 10 01:17:01 python1 CRON[41270]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Mar 10 02:17:01 python1 CRON[76669]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Mar 10 03:17:01 python1 CRON[112074]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Mar 10 06:17:11 python1 systemd[1]: Starting Daily apt upgrade and clean activities...
Mar 10 06:17:11 python1 CRON[5977]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
*** Job1 is delayed **
Mar 10 06:17:11 python1 CRON[5998]: (developer) CMD (sh /home/developer/script/batch_load1.sh >> /home/developer/script/log/batch_load1.log 2>&1)
Mar 10 06:17:13 python1 apt.systemd.daily[5972]: Traceback (most recent call last):
Mar 10 06:17:13 python1 apt.systemd.daily[5972]: File "/usr/bin/unattended-upgrade", line 71, in <module>
Mar 10 06:17:13 python1 apt.systemd.daily[5972]: import apt_inst
Mar 10 06:17:13 python1 apt.systemd.daily[5972]: ModuleNotFoundError: No module named 'apt_inst'
Mar 10 06:17:13 python1 systemd[1]: Started Daily apt upgrade and clean activities.
*** Job2 is delayed ***
Mar 10 06:17:32 python1 CRON[6271]: (developer) CMD (sh /home/developer/script/batch_load2.sh >> /home/developer/script/log/batch_load2.log 2>&1)
Mar 10 06:25:01 python1 CRON[11357]: (root) CMD (test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ))
Mar 10 06:25:16 python1 snapd[126844]: storehelpers.go:436: cannot refresh snap "core": snap has no updates available
Mar 10 06:25:16 python1 snapd[126844]: autorefresh.go:397: auto-refresh: all snaps are up-to-date
*** Job3 ran at expected schedule***
Mar 10 06:30:01 python1 CRON[14434]: (developer) CMD (sh /home/developer/script/batch_load3.sh >> /home/developer/script/log/batch_load3.log 2>&1)
Mar 10 07:17:01 python1 CRON[41944]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Mar 10 08:17:01 python1 CRON[76956]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Mar 10 09:17:01 python1 CRON[112106]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)
Can anyone give me some hints for this strange behavior in crontab? Thanks in advance.
cron.hourly
entries after03:17:01
until06:17:11
- is it possible that the system suspended or hibernated for a couple of hours?