大网数据广告活动
服务器运维问答

Cron job execute every 45 days possible? (approx. 1 and a half months)

· 52CE 我爱测

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.

问题原文

原帖链接 · 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。