如何使用 PHP 启动/停止 cronjob?
如果我在命令行中输入 crontab -l
我可以看到以下行:
# * * * * */usr/bin/php/home/user/every_minute_script.php
要开始这个cronjob,我需要使用crontab -e
命令编辑文件,删除行首的注释字符,保存编辑的文件,并退出编辑器.
要停止这个cronjob,步骤相同,但在行首添加注释字符.
我想使用 PHP 脚本实现完全相同的效果,而不是手动编辑文件.
解决方案我做了一些研究,在一个论坛中找到
a>,以下消息:
<块引用>用编辑器调用crontab -e"环境变量设置为 php脚本.该脚本可以修改文件,当它退出 crontab 将重新读取文件并更新.
所以,我尝试了一些东西,并且奏效了.我将粘贴下面的工作代码:
#!/usr/bin/php<?php$on = "* * * * */usr/bin/php/home/user/every_minute_script.php
";$off = "# * * * * */usr/bin/php/home/user/every_minute_script.php
";$param = isset( $argv[1] ) ?$argv[1] : '';$filename = isset( $argv[2] ) ?$argv[2] : '';如果 ( $param == '激活' ){shell_exec('export EDITOR="/home/user/cron.php on"; crontab -e' );}elseif( $param == 'deactivate' ){shell_exec('export EDITOR="/home/user/cron.php off"; crontab -e');}elseif( in_array( $param, array( 'on', 'off' ) ) ){如果 ( !is_writable( $filename ) )出口();$crontab = 文件( $文件名);$key = array_search( $param == 'on' ? $off : $on, $crontab );如果( $key === false )出口();$crontab[$key] = $param == 'on' ?$on : $off;睡眠(1);file_put_contents( $filename, implode( '', $crontab ) );}出口();?>
实际上,我们有一个名为 cron.php
的脚本位于 /home/user
文件夹,设置为可执行(chmod a+xcron.php
) 并从命令行 (PHP-CLI) 调用.稍后我会调整它以从网络上运行,这是我的意图.
用法:./cron.php activate
启用cronjob 和./cron.php 停用
禁用它.
脚本正确设置 EDITOR 环境变量(对其自身),然后调用 crontab -e
,然后调用 EDITOR(现在是相同的 cron.php 脚本)传递临时变量crontab 文件位置作为参数.然后,找到并更改正确的 crontab 行,并保存修改后的版本,替换临时文件.当脚本退出时,crontab 会更新.
这正是我想要的,并且符合我的需求.
其他答案很好,可能适合不同的需求和场景,我要感谢所有做出贡献的人.
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.
解决方案I did some research and found in a forum, the following message:
Call "crontab -e" with the EDITOR environment variable set to a php script. That script can modify the file and when it exits crontab will re-read the file and update.
So, I have tried something, and it worked. I will paste the working code below:
#!/usr/bin/php
<?php
$on = "* * * * * /usr/bin/php /home/user/every_minute_script.php
";
$off = "# * * * * * /usr/bin/php /home/user/every_minute_script.php
";
$param = isset( $argv[1] ) ? $argv[1] : '';
$filename = isset( $argv[2] ) ? $argv[2] : '';
if ( $param == 'activate' )
{
shell_exec( 'export EDITOR="/home/user/cron.php on"; crontab -e' );
}
elseif( $param == 'deactivate' )
{
shell_exec( 'export EDITOR="/home/user/cron.php off"; crontab -e' );
}
elseif( in_array( $param, array( 'on', 'off' ) ) )
{
if ( !is_writable( $filename ) )
exit();
$crontab = file( $filename );
$key = array_search( $param == 'on' ? $off : $on, $crontab );
if ( $key === false )
exit();
$crontab[$key] = $param == 'on' ? $on : $off;
sleep( 1 );
file_put_contents( $filename, implode( '', $crontab ) );
}
exit();
?>
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.
相关文章