更动态的 Cronjobs
More dynamic cronjobs

原始链接: https://george.mand.is/2025/09/more-dynamic-cronjobs/

## 超越基本调度:动态 Cronjobs 本文重点介绍了一种简单但强大的技术,用于扩展 cronjobs 的功能,使其超越标准的调度限制。虽然 cronjobs 传统上用于重复性任务,但通过在 crontab 条目中直接加入条件检查,可以使其变得非常动态。 作者意识到,你可以利用 POSIX “test” 命令(使用 `[]` 或 `test`)来添加逻辑。例如,可以安排一个脚本在每周二早上 7 点运行,*除非* 是当月的最后一个星期二,通过检查七天后的日期是否仍然在同一个月内来实现。 除此之外,文章还演示了如何根据外部数据触发任务:仅在美國节假日(使用下载的节假日列表)运行脚本,根据天气状况(通过国家气象局 API),甚至由人工智能对当前新闻事件的评估来确定。 本质上,cronjobs 不仅限于基于时间的触发;它们可以响应几乎任何你可以编写脚本的条件,从而释放新的自动化灵活性。

黑客新闻 新的 | 过去的 | 评论 | 提问 | 展示 | 工作 | 提交 登录 更多 动态 Cron 任务 (mand.is) 8 分,由 0928374082 1 小时前发布 | 隐藏 | 过去的 | 收藏 | 1 条评论 victorbjorklund 28 分钟前 [–] 很酷。不知道 CRON 表达式内部可以运行命令。回复 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系 搜索:
相关文章

原文

• ~600 words • 3 minute read

I remember learning about cronjobs in the early 2000s. I could tell the computer to go do something, on a recurring basis, forever, even when I wasn't there. They felt like magic!

We didn't have Crontab.guru or AI to ask for figuring out some of the more complex specifications. Just the man pages and good old-fashioned trial and error—mostly error in my case.

But while you could do fun, complex specifications of recurring intervals, you couldn't quite specify something quite as dynamic as "run this script every Tuesday at 7am unless it's the last Tuesday of the month..."

Or at least, you couldn't strictly through the crontab specification syntax. But I had a recent, mildly embarrassing epiphany that it's not hard at all to add arbitrary checks to your crontab to account for more complex and dynamic scenarios.

Want to run a script every Tuesday of the month at 7am except for the last Tuesday? That's easy—set up your crontab to run every Tuesday at 7am, but add a little check to make sure the next week is still part of the same month:

0 7 * * Tue [ "$(date -v+7d '+%m')" = "$(date '+%m')" ] && /path/to/your_command

If it's not part of the same month, that means we're on the last Tuesday for the month and the script won't run.

Note: The -v flag is for the macOS/BSD flavors of date. On Linux you'd want to use -d +7 days instead.

This really has nothing to do with cronjobs at all and everything to do with the POSIX "test" command which is the thing we're using with those square brackets. I'm used to seeing and utilizing them in shell scripts, but for whatever reason I never thought to reach for that tool here in the crontab.

You could just as easily rewrite it like this, skipping the bracket shorthand, which is probably easier to read:

0 7 * * Tue test "$(date -v+7d '+%m')" = "$(date '+%m')" && /path/to/your_command

It never crossed my mind until recently to add slightly more complex checks at the crontab level.

Other clever cronjob things you can do:

Holiday-only cronjobs

Maybe fetch a list of all the US Holidays for a given year and store them in a handy HOLIDAYS.txt file somewhere:

curl -s https://date.nager.at/api/v3/PublicHolidays/2025/US | jq -r '.[].date' > HOLIDAYS.txt

Now you can update your cronjob to run every Tuesday at 7am except on Holidays:

0 7 * * Tue ! grep -qx "$(date +%F)" HOLIDAYS.txt && /path/to/your_command

Or inversely, maybe run a holiday-only script that checks once a day

@daily grep -qx "$(date +%F)" HOLIDAYS.txt && /path/to/your_special_holiday_command

Only run on sunny days

The National Weather Service makes all kinds of fun data available (if you can find it...). How about a script that runs every hour, but only when the weather is clear?

@hourly curl -s "https://api.weather.gov/gridpoints/TOP/32,81/forecast/hourly" | jq -r '.properties.periods[0].shortForecast' | grep -qi clear && /path/to/your_command

Or maybe when the weather is cloudy?

@hourly curl -s "https://api.weather.gov/gridpoints/TOP/32,81/forecast/hourly" | jq -r '.properties.periods[0].shortForecast' | grep -qi cloudy && /path/to/your_command

Only run when there's something newsworthy

Or maybe we get in line with every-other-startup I'm aware of and throw AI at the problem, only running our script when the LLM gods have decided there is something newsworthy:

@hourly curl -s "https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en" | llm --system "Reply strictly 'yes' or 'no'. Does anything in the news today suggest it is a good reason to run a script that I only want to send when the world is on fire and crazy and terrible things are happening?"  | tr -d '[:space:]' | tr '[:upper:]' '[:lower:]' | grep -qx yes && /path/to/oh_no
--

Published on Sunday, September 21st 2025. Read this post as Markdown or plain-text.

If you enjoyed reading this consider signing-up for my newsletter, sharing it on Hacker News or hiring me.

联系我们 contact @ memedata.com