【发布时间】:2012-03-30 17:08:22
【问题描述】:
我将按计划通过 curl 触发对 PHP 文件的调用。我正在考虑让脚本每 23:59:59 执行一次,或者只是在明天一天之前的一分钟执行一次。有什么最好的方法吗?对 cron 设置仍然很困惑。
我需要确保我在第二天前一秒跑完。
【问题讨论】:
-
我完全同情,cron 设置可能相当混乱。我会在几秒钟内发布答案:-)
我将按计划通过 curl 触发对 PHP 文件的调用。我正在考虑让脚本每 23:59:59 执行一次,或者只是在明天一天之前的一分钟执行一次。有什么最好的方法吗?对 cron 设置仍然很困惑。
我需要确保我在第二天前一秒跑完。
【问题讨论】:
Minutes [0-59]
| Hours [0-23]
| | Days [1-31]
| | | Months [1-12]
| | | | Days of the Week [Numeric, 0-6]
| | | | |
* * * * * home/path/to/command/the_command.sh
59 23 * * * home/path/to/command/the_command.sh
【讨论】:
要在每天 23:59:00 执行脚本,请使用以下命令:
59 23 * * * root /path_to_file_from_root
无法使用 Cron 定义秒数,但这应该适合您。
要在 23:59:59 执行脚本,请使用 PHP sleep() 函数将脚本的执行延迟 59 秒。不过我建议 58 秒,以确保脚本不会延迟到午夜之后。
这是非常基本的,您可以让它变得更复杂一些并运行测试,以确保脚本始终在 23:59:59 运行,方法是检索时间并适当延迟。不过这应该不是必需的。
<?php
// Any work that the script can do without altering the database, do here
// Delay the script by 58 seconds
sleep(58);
// Carry on with the rest of the script here, database updates etc
?>
【讨论】:
启动 crontab 编辑
crontab -e
或通过
vi /etc/cronatb
这取决于发行版。
59 23 * * * root /path/to/your/php/file.php
请注意,“root”列表示开始工作的用户名,可能在您的系统上不可用。
尝试运行
man crontab
更多详情
【讨论】:
您可以将其设置为每天 23:59:00 运行,请查看 here 中的示例。具体看例子号。 1,它准确地解释了如何为每天的特定时间设置一个 cronjob。
【讨论】: