我想执行一项cron作业,该作业将每月和周一每20天运行一次。
从一个星期一到另一个星期一应该有20天的间隔。
例如-
如果Cron作业在本月星期一运行 意味着下一个工作将在10月12日进行,而下一个工作将在11月2日和星期一进行。
帮帮我。我试图但无法实现。
答案 0 :(得分:1)
此工具可能可以帮助您:crontab.guru
如果我理解您的问题是这样的:
#+--------------------------- Minute (0-59)
#| +---------------------- Hour (0-23)
#| | +---------------- Day (1-31)
#| | | +------------ Month (1-12)
#| | | | +-------- Day of week (0-6, 0=Sunday)
#| | | | | +--- Command to be run
#| | | | | |
#v v v v v v
#====================================================================
# run task At 00:00 on Monday.
0 0 * * 1 run_task.sh
您可以在run_task.sh
中输入条件。
# script to check if the task run 20 days ago.
aux_file="/tmp/auxdate.txt"
# this compare the actual date with the date in the file
# if the diference is >= 20 the task run
if [[ ! -e "$aux_file" || $(( ($(date '+%s') - $(cat auxdate.txt))/(60*60*24) )) -ge 20 ]]; then
echo $(date '+%s') > "$aux_file"
echo "RUN TASK"
# run task...
else
echo "NOT RUN TASK"
fi