本文介绍了如何使用PHP启动/停止cronjob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在命令行中键入 crontab -l ,我可以看到以下行:

 #* * * * * / usr / bin / php /home/user/every_minute_script.php 

开始此cronjob,我需要使用 crontab -e 命令编辑文件,删除

若要停止此cronjob,同样的步骤,但添加注释



我想使用 PHP脚本实现完全相同的效果,而不是手动编辑文件。

解决方案

我做了一些研究,并且中找到以下消息:

所以,我尝试了一些东西,它的工作。我将粘贴以下工作代码:

 #!/ 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))
出口();

$ 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();

?>

就像这样,我们有一个名为 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会更新。



这正是我想要的,符合我的需要。



其他答案很不错,可能适合不同的需求和场景,我要感谢所有贡献的人。


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:

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\n";
$off = "# * * * * * /usr/bin/php /home/user/every_minute_script.php\n";

$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.

这篇关于如何使用PHP启动/停止cronjob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 23:38