使用 cron 运行 php 脚本来安排事件

发布于 2024-12-25 11:47:33 字数 793 浏览 4 评论 0原文

我正在尝试在每月的三个不同时间、每天、每个星期一和该月的第一个星期一运行不同的脚本,我在这里的人们的帮助下整理了以下内容,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最偏执的依靠 2025-01-01 11:47:33

我只需手动设置这些变量来测试它:

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

将其更改为您想要测试并触发它的任何值 - 看看会发生什么!

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!

垂暮老矣 2025-01-01 11:47:33

我建议使用几乎每个人都用于此类工作的工具:cron。

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

一个人的旅程 2025-01-01 11:47:33

我不会添加 php 层来执行此操作。 Cron 可以自行处理这种调度。

这应该可以做到:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文