【发布时间】:2017-05-16 04:58:57
【问题描述】:
我在上午 9 点、上午 9 点 15 分、上午 10 点有一个约会列表,约会是动态安排的。我必须在预定的时间发送推送通知。
- 因为计划时间是动态的或可由 HR 编辑。
- 我每隔一分钟运行一次 cron,如果我发现计划的时间低于 ,则该 cron 每隔一分钟运行一次。
请提出执行计划,因为我认为这不是最佳解决方案。
【问题讨论】:
我在上午 9 点、上午 9 点 15 分、上午 10 点有一个约会列表,约会是动态安排的。我必须在预定的时间发送推送通知。
请提出执行计划,因为我认为这不是最佳解决方案。
【问题讨论】:
您遵循的方法是正确的。由于约会是动态安排的,因此可以随时进行。每分钟运行一次 cron 并在您的脚本中检查是否有任何约会的日程安排时间已到并且通知已发送为 false。为这些约会触发通知,并将发送的通知设置为 true。
【讨论】:
直接从 PHP 编辑 crontab
我在资源有限的系统上使用的一个技巧是编辑 crontab 本身,而不是每 60 秒不断调用 PHP 脚本。 将您的 crontab 转换为 PHP 数组可以轻松安排任务。
添加任务可以使用array_push来完成
<?PHP
$newcron='00 09 * * * /usr/bin/PHP /fullPath/myPhpScript.php'; // New time and task
$crons= explode( PHP_EOL ,shell_exec('crontab -l'));
array_push($crons,$newcron);
file_put_contents('/tmp/crontab.txt', implode (PHP_EOL,$crons));
echo exec('crontab /tmp/crontab.txt');
?>
可以使用 unset 完成删除任务
<?PHP
$crons= explode( PHP_EOL ,shell_exec('crontab -l'));
unset($crons[1]); //Task entry to remove
file_put_contents('/tmp/crontab.txt', implode (PHP_EOL,$crons));
echo exec('crontab /tmp/crontab.txt');
?>
Crontab 格式为:
* * * * * /usr/bin/php /fullPath/myPhpScript.php
| | | | | |
| | | | | +-- Command to call your script
| | | | +---- Day of the Week (range: 1-7, 1 standing for Monday)
| | | +------ Month of the Year (range: 1-12)
| | +-------- Day of the Month (range: 1-31)
| +---------- Hour (range: 0-23)
+------------ Minute (range: 0-59)
如果您的约会是在上午 9:00、9:15 和 10:00,您可以从类似于下面的 crontab 开始。
00 09 * * * /usr/bin/PHP /fullPath/0900PushNotifyList.php
15 09 * * * /usr/bin/PHP /fullPath/0915PushNotifyList.php
00 10 * * * /usr/bin/PHP /fullPath/1000PushNotifyList.php
有关 crontab 命令和格式的更多信息,请访问https://stackoverflow.com/tags/crontab/info
【讨论】: