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

2023-08-17php开发问题
12

本文介绍了如何使用 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用

PHP实现DeepL翻译API调用

DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期数据处理方法

PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - 无法使用 codeigniter 发送电子邮件

mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误

Laravel Gmail 配置错误

Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题

将 PHPMailer 用于 SMTP 的问题

Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题

Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17

热门文章

1nohup:忽略输入并将输出附加到“nohup.out" 2在控制台中出错:无法加载资源:net::ERR_CONNECTION_RESET 3如何将 LDAP 时间戳转换为 Unix 时间戳 4不推荐使用常量 FILTER_SANITIZE_STRING 5APACHE 崩溃:父进程:子进程以状态 3221225477 退出 -- 正在重新启动 6PHP通过phpspreadsheet导入Excel日期数据处理方法 7Analytics API 返回:错误请求 - invalid_grant 8“tlsv1 警报内部错误"握手时

热门精品源码

最新VIP资源

1多功能实用站长工具箱html功能模板 2多风格简历在线生成程序网页模板 3论文相似度查询系统源码 4响应式旅游景点宣传推广页面模板 5在线起名宣传推广网站源码 6酷黑微信小程序网站开发宣传页模板 7房产销售交易中介网站模板 8小学作业自动生成程序