我将在计划的基础上通过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
请注意,"根"列表示启动作业的用户名称,可能在您的系统中不可用。
尝试运行
man crontab
有关更多详细信息,
您可以将其设置为每天23:59:00运行,看看这里的示例。具体来看示例1,它准确地解释了如何为每天的特定时间设置cronjob。