If I type crontab -l
in the command-line I can see the following line:
如果我在命令行中输入crontab -l,我可以看到如下一行:
# * * * * * /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.
要启动这个cronjob,我需要使用crontab -e命令编辑文件,删除行开头的注释字符,保存已编辑的文件,然后退出编辑器。
To stop this 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脚本实现完全相同的效果,而不是手工编辑文件。
5 个解决方案
#1
11
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.
调用“crontab -e”,并将编辑器环境变量设置为php脚本。该脚本可以修改文件,当文件退出时,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.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.
实际上,我们有一个名为cron的脚本。php位于/home/user文件夹,设置为可执行(chmod a+x cron.php),并从命令行(php - cli)调用。稍后我将对它进行微调,使其从web上运行,这是我的意图。
Usage: ./cron.php activate
to enable the cronjob and ./cron.php deactivate
to disable it.
用法:。/ cron。php激活以启用cronjob和./cron。php禁用它。
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.
该脚本适当地(对自身)设置编辑器环境变量,然后调用crontab -e,它反过来调用编辑器(现在是相同的cron)。将临时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.
其他的答案很好,可能适合不同的需求和场景,我想感谢所有的贡献者。
#2
7
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:
我建议您在every_minute_script.php中进行检查,而不是在程序上使用crontab(它很微妙,很容易让人生气)。
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_script
doesn't exist), it will terminate immediately without further processing.
这样,脚本仍然会每分钟开始,但是如果条件没有满足(/您/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).
然后,包装器脚本运行该作业,收集其输出和错误,如果这些错误不是空的,就将它们发送出去(注意,您也可以在包装器脚本中进行检查;我们没有,因为它依赖于数据库)。
#3
6
Here's a pretty cool tutorial for creating exactly this kind of functionality with PHP.
这里有一个非常酷的教程,可以用PHP创建这种功能。
#4
2
Put your stop/start condition at the start of every_minute_script.php
将stop/start条件放在every_minute_script.php的开头
if($condition == false) {
exit();
}
#5
2
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.
您能在every_minute_script的开头放置一些逻辑吗?php检查标志,看看它是否应该做什么?这样的话,它就可以向上旋转,如果没有工作,它就会很快停止。
Or is that too inefficient for your purposes?
或者,这是不是太低效了?
#1
11
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.
调用“crontab -e”,并将编辑器环境变量设置为php脚本。该脚本可以修改文件,当文件退出时,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.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.
实际上,我们有一个名为cron的脚本。php位于/home/user文件夹,设置为可执行(chmod a+x cron.php),并从命令行(php - cli)调用。稍后我将对它进行微调,使其从web上运行,这是我的意图。
Usage: ./cron.php activate
to enable the cronjob and ./cron.php deactivate
to disable it.
用法:。/ cron。php激活以启用cronjob和./cron。php禁用它。
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.
该脚本适当地(对自身)设置编辑器环境变量,然后调用crontab -e,它反过来调用编辑器(现在是相同的cron)。将临时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.
其他的答案很好,可能适合不同的需求和场景,我想感谢所有的贡献者。
#2
7
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:
我建议您在every_minute_script.php中进行检查,而不是在程序上使用crontab(它很微妙,很容易让人生气)。
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_script
doesn't exist), it will terminate immediately without further processing.
这样,脚本仍然会每分钟开始,但是如果条件没有满足(/您/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).
然后,包装器脚本运行该作业,收集其输出和错误,如果这些错误不是空的,就将它们发送出去(注意,您也可以在包装器脚本中进行检查;我们没有,因为它依赖于数据库)。
#3
6
Here's a pretty cool tutorial for creating exactly this kind of functionality with PHP.
这里有一个非常酷的教程,可以用PHP创建这种功能。
#4
2
Put your stop/start condition at the start of every_minute_script.php
将stop/start条件放在every_minute_script.php的开头
if($condition == false) {
exit();
}
#5
2
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.
您能在every_minute_script的开头放置一些逻辑吗?php检查标志,看看它是否应该做什么?这样的话,它就可以向上旋转,如果没有工作,它就会很快停止。
Or is that too inefficient for your purposes?
或者,这是不是太低效了?