有個需求,crontab任務需要每隔1天執行,有倆個方法,如下:
第一種方法,利用crontab執行
0 0 */2 * * shellscript
但是這種方法有個問題,就是有個月份有31天,導致本月31號和下月1號,都會執行此操作,並沒有真正實現每隔兩天周期來執行任務。
第二種方法,可以通過在/tmp目錄下標記文件的方式來實現:
[root@hch ~]# cat sig.sh #!/bin/bash if [ -f /tmp/altday ];then rm /tmp/altday exit 0 else touch /tmp/altday sh /root/insert.sh fi
[root@hch ~]# cat insert.sh #!/bin/bash date >> /tmp/execute.log && echo "execute" >> /tmp/execute.log
將腳本添加至crontab中
[root@hch ~]# crontab -l 18 16 * * * sh /root/sig.sh
參考
如何使用cron任務每隔2天在固定時間執行任務 - Bob Liu的程序人生 - CSDN博客 https://blog.csdn.net/jinguangliu/article/details/83013646