моя ОС - Ubuntu 18.04.3 LTS, у меня есть 2 задания crontab, запланированные на каждое утро 4:00 и 4.05, соответственно.
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
В batch_load1. sh и batch_load2 . sh, они будут читать текстовый файл и запускать сценарий python. Причина, по которой я разделяю 2 sh bash сценариев, заключается в том, что я могу запускать их параллельно и экономить время, поскольку batch_load1. sh займет довольно много времени, чтобы завершить sh.
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 ""
Однако, когда я проверяю batch_load1.log и batch_load2.log, они не запускаются в запланированное время и задерживаются. Задание batch_load1 начинается в 06:17:11
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 начинается в 06:17:32
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.
Если я добавлю другое задание (batch_load3. sh) в расписании, которое запускается каждые 6:30 утра, оно будет работать ровно в 6:30 утра, без задержки:
30 6 * * * sh /home/developer/script/batch_load3.sh >> /home/developer/script/log/batch_load3.log 2>&1
В /var/log/syslog.1 я вижу, что job1 и 2 запускались в 06:17, а job3 - в 06:30 по расписанию:
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)
Кто-нибудь может дать мне несколько советов по поводу этого странного поведения в crontab? Заранее спасибо.