使用 cron 运行 php 脚本来安排事件
我正在尝试在每月的三个不同时间、每天、每个星期一和该月的第一个星期一运行不同的脚本,我在这里的人们的帮助下整理了以下内容,cron 将每天运行一次并且相应的脚本将在预设的时间运行。
除了将其放在服务器上并等待选定的日期之外,我还可以测试以下内容吗?我正在运行本地 apache 服务器,如果我将其日期更改为该月的第一天并运行它,会这样可以吗?
这一切都正确吗? :
<?php
$RUN_everyday = `/path/to/php/everyday.php`;
$RUN_everymonday = `/path/to/php/everymonday.php`;
$RUN_firstmondayofmonth = `/path/to/php/firstmondayofmonth.php`;
date_default_timezone_set('Europe/London');
$weekday = date('D');
$dayOfMonth = date('d');
// runs the everyday php file
echo '$RUN_everyday';
if ($weekday == 'Mon')
{
//runs the every monday php file every monday of the week
echo '$RUN_everymonday';
if ($dayOfMonth <=6)
//runs the first monday of the month php file on the first monday of each month
echo '$RUN_mondayofmonth';
}
?>
I am trying to run a different script on the three different time of the month, every day, every Monday and the first Monday of the month, I have put together the following with help from people on here, cron will run this once a day and the corresponding scripts will get run at the pre set times.
Is there anyway I can test the following other than putting it on the server and waiting for the selected dates ?, I am running a local apache server, if I change the date on that to the first day of the month and run it, would this be ok ?.
Is this all correct ? :
<?php
$RUN_everyday = `/path/to/php/everyday.php`;
$RUN_everymonday = `/path/to/php/everymonday.php`;
$RUN_firstmondayofmonth = `/path/to/php/firstmondayofmonth.php`;
date_default_timezone_set('Europe/London');
$weekday = date('D');
$dayOfMonth = date('d');
// runs the everyday php file
echo '$RUN_everyday';
if ($weekday == 'Mon')
{
//runs the every monday php file every monday of the week
echo '$RUN_everymonday';
if ($dayOfMonth <=6)
//runs the first monday of the month php file on the first monday of each month
echo '$RUN_mondayofmonth';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我只需手动设置这些变量来测试它:
将其更改为您想要测试并触发它的任何值 - 看看会发生什么!
I'd test it by just manually setting those variables:
Change that to whatever values you want to test and trigger it - see what happens!
我建议使用几乎每个人都用于此类工作的工具:cron。
I'd suggest using the tool that almost everybody uses for this type of job: cron.
我不会添加 php 层来执行此操作。 Cron 可以自行处理这种调度。
这应该可以做到:
I wouldn't add the layer of php to do this. Cron can handle this sort of scheduling on its own.
This should do it: