如何使用 PHP 启动/停止 cronjob?
如果我在命令行中输入 crontab -l ,我可以看到以下行:
# * * * * * /usr/bin/php /home/user/every_minute_script.php
To start this cronjob, I need to edit the file using crontab -e 命令,删除行首的注释字符,保存编辑的文件,然后退出编辑器。
要停止此 cronjob,请执行相同的步骤,但在行首添加注释字符。
我想使用 PHP 脚本 实现完全相同的效果,而不是手动编辑文件。
If I type crontab -l
in the command-line I can see the following line:
# * * * * * /usr/bin/php /home/user/every_minute_script.php
To start this cronjob, I need to edit the file using crontab -e
command, remove the comment character at the beginning of the line, save the edited file, and exit the editor.
To stop this cronjob, the same steps, but adding the comment character at the beginning of the line.
I want to achieve exactly the same effect using a PHP script, instead of manually editing the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我做了一些研究并在论坛中发现,以下消息:
所以,我尝试了一些方法,并且成功了。我将粘贴下面的工作代码:
事实上,我们有一个名为
cron.php
的脚本,位于/home/user
文件夹中,设置为可执行文件(chmod a+x cron.php
) 并从命令行 (PHP-CLI) 调用。稍后我将调整它以从网络运行,这就是我的意图。用法:
./cron.php activate
启用 cronjob,./cron.php deactivate
禁用它。该脚本正确设置 EDITOR 环境变量(对其自身),然后调用 crontab -e,后者又调用 EDITOR(现在是相同的 cron.php 脚本)并传递临时 crontab 文件位置作为一个论点。然后,找到并更改正确的 crontab 行,并保存修改后的版本,替换临时文件。当脚本退出时,crontab 将更新。
这正是我想要的,并且符合我的需求。
其他答案都很好,可能适合不同的需求和场景,我要感谢所有做出贡献的人。
I did some research and found in a forum, the following message:
So, I have tried something, and it worked. I will paste the working code below:
As it is, we have a single script named
cron.php
located at/home/user
folder, set to be executable (chmod a+x cron.php
) and called from the command-line (PHP-CLI). Later I will tweak it to run from the web, which is my intent.Usage:
./cron.php activate
to enable the cronjob and./cron.php deactivate
to disable it.The script sets the EDITOR environment variable properly (to itself) and then calls
crontab -e
, which on its turn calls the EDITOR (which is now the same cron.php script) passing the temporary crontab file location as an argument. Then, the proper crontab line is found and changed, and the modified version is saved, substituting the temporary file. When the script exits, crontab will update.This does exactly what I wanted, and fit my needs.
The other answers are nice and may fit different needs and scenarios, and I want to thank everybody who contributed.
我建议不要以编程方式搞乱 crontab (这是微妙且容易生气的),而是建议在 every_month_script.php 中进行检查:
这样,脚本仍然会每分钟启动,但如果不满足条件(< code>/your/path/run_every_minute_script 不存在),它将立即终止而不进行进一步处理。
(当然,您可以在那里替换不同的条件,例如检查数据库的权限等。)
如果您需要邮寄输出,您可以使用包装脚本。 Crontab:
然后包装器脚本正确运行作业,收集其输出和错误,如果这些不为空,则将它们邮寄出去(请注意,您也可以在包装器脚本内进行检查;我们没有,因为它依赖于在数据库上)。
Rather than messing programatically with crontab (which is subtle and quick to anger), I'd suggest making a check inside your every_minute_script.php:
This way, the script will still start every minute, but if the condition isn't met (
/your/path/run_every_minute_script
doesn't exist), it will terminate immediately without further processing.(Of course, you can substitute different conditions there, e.g. checking the database for permission etc.)
If you need the output mailed, you can use a wrapper script. Crontab:
The wrapper script then runs the job proper, collects its output and errors, and if those aren't empty, mails them out (note that you could also make the check inside the wrapper script; we didn't, as it relied on database).
这里一个非常酷的教程,用于准确创建PHP 具有此类功能。
Here's a pretty cool tutorial for creating exactly this kind of functionality with PHP.
将停止/开始条件放在
every_Minute_script.php
的开头Put your stop/start condition at the start of
every_minute_script.php
您能否在
every_minute_script.php
的开头放置某种逻辑来检查标志以查看它是否应该执行任何操作?这样它就可以旋转起来,然后在没有工作可做的情况下快速停止。或者这对于您的目的来说太低效了?
Could you place some kind of logic at the beginning of
every_minute_script.php
that checks a flag to see if it should do anything? That way it could spin up and then just quickly stop if there is no work to do.Or is that too inefficient for your purposes?