我的操作系统是 Ubuntu 18.04.3 LTS。我有两个 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
完成需要相当长的时间。
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
,我可以看到作业 1 和 2 于上午 06:17 运行,而作业 3 于上午 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 中这种奇怪行为的提示吗?提前谢谢。