<bdo id='otjSf'></bdo><ul id='otjSf'></ul>
<legend id='otjSf'><style id='otjSf'><dir id='otjSf'><q id='otjSf'></q></dir></style></legend>
  • <small id='otjSf'></small><noframes id='otjSf'>

    1. <i id='otjSf'><tr id='otjSf'><dt id='otjSf'><q id='otjSf'><span id='otjSf'><b id='otjSf'><form id='otjSf'><ins id='otjSf'></ins><ul id='otjSf'></ul><sub id='otjSf'></sub></form><legend id='otjSf'></legend><bdo id='otjSf'><pre id='otjSf'><center id='otjSf'></center></pre></bdo></b><th id='otjSf'></th></span></q></dt></tr></i><div id='otjSf'><tfoot id='otjSf'></tfoot><dl id='otjSf'><fieldset id='otjSf'></fieldset></dl></div>
      <tfoot id='otjSf'></tfoot>

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

        时间:2023-08-17
          • <bdo id='wePf9'></bdo><ul id='wePf9'></ul>

              <small id='wePf9'></small><noframes id='wePf9'>

              <legend id='wePf9'><style id='wePf9'><dir id='wePf9'><q id='wePf9'></q></dir></style></legend>

            • <i id='wePf9'><tr id='wePf9'><dt id='wePf9'><q id='wePf9'><span id='wePf9'><b id='wePf9'><form id='wePf9'><ins id='wePf9'></ins><ul id='wePf9'></ul><sub id='wePf9'></sub></form><legend id='wePf9'></legend><bdo id='wePf9'><pre id='wePf9'><center id='wePf9'></center></pre></bdo></b><th id='wePf9'></th></span></q></dt></tr></i><div id='wePf9'><tfoot id='wePf9'></tfoot><dl id='wePf9'><fieldset id='wePf9'></fieldset></dl></div>
              <tfoot id='wePf9'></tfoot>

                    <tbody id='wePf9'></tbody>
                1. 本文介绍了如何使用 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.

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

                  上一篇:PHP:如何压缩图像而不损失可见质量(自动)? 下一篇:PHP - 在 Cron 中防止碰撞 - 文件锁定安全吗?

                  相关文章

                2. <tfoot id='A1Wlh'></tfoot>
                3. <small id='A1Wlh'></small><noframes id='A1Wlh'>

                    1. <i id='A1Wlh'><tr id='A1Wlh'><dt id='A1Wlh'><q id='A1Wlh'><span id='A1Wlh'><b id='A1Wlh'><form id='A1Wlh'><ins id='A1Wlh'></ins><ul id='A1Wlh'></ul><sub id='A1Wlh'></sub></form><legend id='A1Wlh'></legend><bdo id='A1Wlh'><pre id='A1Wlh'><center id='A1Wlh'></center></pre></bdo></b><th id='A1Wlh'></th></span></q></dt></tr></i><div id='A1Wlh'><tfoot id='A1Wlh'></tfoot><dl id='A1Wlh'><fieldset id='A1Wlh'></fieldset></dl></div>
                    2. <legend id='A1Wlh'><style id='A1Wlh'><dir id='A1Wlh'><q id='A1Wlh'></q></dir></style></legend>

                        <bdo id='A1Wlh'></bdo><ul id='A1Wlh'></ul>