Cron 作业在特定时间启动和停止

发布于 2024-10-07 14:36:58 字数 132 浏览 3 评论 0原文

是否可以设置一个 cron 作业只在某些日子的晚上工作,例如每个月的第一天下午 3 点到晚上 9 点?

我只想在晚上服务器负载较低时进行电子邮件营销活动。

是否可以在某个时间运行一个 cron 作业并在某个时间停止它?

Is it possible to setup a cron job to work on certain days only at night, for example, the first day of each month from 03pm to 09pm?

I want to do an email campaign only at night when the server load is low.

Is it possible to run a cron job at a certain time and stop it at a certain time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软甜啾 2024-10-14 14:36:58

您可以在晚上 9 点使用第二个 cron 作业来启动第二个程序,告诉第一个程序终止。

有多种方法可以做到这一点。最简单的方法之一可能是让第二个程序在方便的地方触摸 terminate.txt。在第一个程序的每个循环中,它可以检查该文件。如果存在,它可以删除该文件并正常退出。

You could use a second cron job at 09pm to start a second program that tells the first program to terminate.

There are a number of ways to do this. One of the easiest might be to have the second program touch terminate.txt in a convenient place. On each loop of the first program, it could check for this file. If it exists, it could delete the file and gracefully exit.

梦境 2024-10-14 14:36:58

将您的 cron 作业设置为每天在所需的时间运行。然后添加一个测试命令来检查是否是该月的第一天。

  • 确保您的脚本仅处理某些工作块,然后自行退出。
  • 确保该脚本验证其自身只有一个实例正在运行。

因此,您的 cron 作业条目将如下所示:

* 15-20 * * * test `date +\%d` == 01 && script_name.sh

脚本将在下午 3:00 到晚上 8:59(含)之间的每分钟启动。

Set up your cron job to run at desired time every day. Then add a test command to check if it is a first day of the month.

  • Make sure your script only processes certain chunk of work and then exits on its own.
  • Make sure that the script verifies that only one instance of itself is running.

So your cron job entry would be something like this:

* 15-20 * * * test `date +\%d` == 01 && script_name.sh

The script will be started at every minute between 3:00 pm and 8:59 pm (inclusive).

如果没结果 2024-10-14 14:36:58

并不真地。您可能可以设置一些东西来在六个小时后终止正在运行的脚本,但这通常不是一个好的解决方案,因为您的脚本没有机会清理东西。

最好的办法是在达到时间限制时从脚本内部彻底退出。

Not really. You could probably set something up to terminate a running script after six hours, but that usually wouldn't be a good solution because your script has no chance of cleaning things up.

The best thing to do would be to do a clean exit from inside the script when the time limit has been reached.

壹場煙雨 2024-10-14 14:36:58

您可以安排 cron 在每月第一天下午 3 点启动您的程序。你的程序必须在晚上 9 点自行终止。

http://en.wikipedia.org/wiki/Cron

You can schedule cron to start your program on the first day of the month at 3PM. Your program will have to take care of killing itself at 9PM.

http://en.wikipedia.org/wiki/Cron

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文