run a php script with cron that schedules events

若如初见. 提交于 2019-12-11 07:48:24

问题


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';
}
?>

回答1:


I'd test it by just manually setting those variables:

date_default_timezone_set('Europe/London');
//$weekday = date('D');
//$dayOfMonth = date('d');
$weekday = 4;      // <-----------
$dayOfMonth = 14;  // <-----------
// runs the everyday php file
echo '$RUN_everyday';

Change that to whatever values you want to test and trigger it - see what happens!




回答2:


I'd suggest using the tool that almost everybody uses for this type of job: cron.




回答3:


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:

0 0 * * * php every_day_midnight.php
0 0 * * 1 php every_monday_midnight.php
0 0 0-6 * 1 php first_monday_midnight.php


来源:https://stackoverflow.com/questions/8781760/run-a-php-script-with-cron-that-schedules-events

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!