问题原文
原帖链接 · GustavMahler · CC BY-SA 3.0
After reading the crontab documentation, I can't think of a way of notating the following schedule: execute script exactly once every 45 days.
Does anyone know of am elegant solution? Thank you.
已采纳解答
原帖链接 · kasperd · CC BY-SA 3.0
Because a period of 45 days does not divide nicely into the weeks, months, or years of the Gregorian calendar there is no way to restrict which dates your script runs on and still have it execute on all of the days which you want.
Because of that you will need to have your command run every single day and have the command itself return without doing anything on 44 out of every 45 days.
For example if you want the script to run at 03:17 the command could look like this:
17 3 * * * /usr/local/bin/your-script
And inside the script you could do this:
#!/bin/bash
if [[ $[($(date +%s)/86400)%45] != 0 ]]
then
return
fi
real things happen here
The != 0 part can use a different number depending on which day you want it to run the first time. And you can choose to put that extra logic in the crontab rather than in the script, though I personally find it more readable to do it in the script.
Notice that if your timezone uses DST you may need to tweak the above to avoid the schedule shifting by a day when the clock is adjusted. The exact details depend on which timezone and which time of day you choose to run your script.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
