Linux 如何使用 PHP 启动/停止 cronjob?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6548746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:52:41  来源:igfitidea点击:

How to start/stop a cronjob using PHP?

phplinuxcroncrontab

提问by J. Bruni

If I type crontab -lin the command-line I can see the following line:

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

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

To startthis cronjob, I need to edit the file using crontab -ecommand, remove the comment character at the beginning of the line, save the edited file, and exit the editor.

启动这个 cronjob,我需要使用crontab -e命令编辑文件,删除行首的注释字符,保存编辑的文件,然后退出编辑器。

To stopthis cronjob, the same steps, but adding the comment character at the beginning of the line.

停止此 cronjob,步骤相同,但在行首添加注释字符。

I want to achieve exactly the same effect using a PHP script, instead of manually editing the file.

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

采纳答案by J. Bruni

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.

使用设置为 php 脚本的 EDITOR 环境变量调用“crontab -e”。该脚本可以修改文件,当它退出时 crontab 将重新读取文件并更新。

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.phplocated at /home/userfolder, 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.

正因为如此,我们有一个名为单个脚本cron.php位于/home/user文件夹设置为可执行文件(chmod a+x cron.php)和命令行(PHP-CLI)调用。稍后我将调整它以从网络运行,这是我的意图。

Usage: ./cron.php activateto enable the cronjob and ./cron.php deactivateto disable it.

用法:./cron.php activate启用和./cron.php deactivate禁用cronjob 。

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.

该脚本正确设置了 EDITOR 环境变量(对其自身),然后调用crontab -e,后者又调用 EDITOR(现在是相同的 cron.php 脚本)将临时 crontab 文件位置作为参数传递。然后,找到并更改正确的 crontab 行,并保存修改后的版本,替换临时文件。当脚本退出时,crontab 将更新。

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.

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

回答by Adam Hopkinson

Put your stop/start condition at the start of every_minute_script.php

将您的停止/启动条件放在 every_minute_script.php

if($condition == false) {
    exit();
}

回答by Justin Ethier

Could you place some kind of logic at the beginning of every_minute_script.phpthat 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.

您能否在开始时放置某种逻辑every_minute_script.php来检查标志以查看它是否应该执行任何操作?这样它就可以旋转,然后在没有工作要做的情况下快速停止。

Or is that too inefficient for your purposes?

或者这对您的目的来说效率太低了吗?

回答by Uku Loskit

Here'sa pretty cool tutorial for creating exactly this kind of functionality with PHP.

这是一个非常酷的教程,用于使用 PHP 创建这种功能。

回答by Piskvor left the building

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:

与其以编程方式弄乱 crontab(这很微妙而且很容易生气),我建议在您的 every_minute_script.php 中进行检查:

if (!file_exists('/your/path/run_every_minute_script')) {
  exit;
}

This way, the script will still start every minute, but if the condition isn't met (/your/path/run_every_minute_scriptdoesn't exist), it will terminate immediately without further processing.

这样,脚本仍将每分钟启动一次,但如果不满足条件(/your/path/run_every_minute_script不存在),它将立即终止而不进行进一步处理。

(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:

如果您需要邮寄输出,您可以使用包装脚本。定时盘:

* * * * * /your/path/wrapper.sh > /dev/null 2> /dev/null

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

然后包装脚本正确运行作业,收集其输出和错误,如果这些不是空的,则将它们邮寄出去(注意,您也可以在包装脚本内进行检查;我们没有,因为它依赖于数据库)。