php

使用 PHP 管理 cron 作业

dafenqi
2023-08-04 / 0 评论 / 18 阅读 / 正在检测是否收录...

使用 PHP 管理 cron 作业

crontab 或 cron 表是一个 Linux 系统进程/守护进程,有助于调度重复性任务,从而简化我们的日常工作。在本教程中,我们将创建一个动态 PHP 类,它允许我们使用安全连接来操作 crontab。

背景:Crontab概述
能够安排任务在后台运行真是太棒了!备份 SQL 数据库、获取或发送电子邮件、运行清理任务、分析性能,甚至获取 RSS 源 - cron 作业非常棒!

尽管计划新作业的语法乍一看似乎令人生畏,但一旦分解它,就相对容易理解。cron 作业将始终有五列,每列代表一个按时间顺序排列的运算符,后跟要执行的完整路径和命令:

          • home/path/to/command/the_command.sh
            按时间顺序排列的每个列都与任务计划具有特定的相关性。它们如下:

分钟表示给定小时的分钟数,分别为 0-59。
小时表示给定一天的小时数,分别为 0-23。
天表示给定月份的天数,分别为 1-31。
月表示给定年份的月份,分别为 1-12。
星期几表示星期几,星期日到星期六,分别在数字上为 0-6。
Minutes [0-59]
| Hours [0-23]
| | Days [1-31]
| | | Months [1-12]
| | | | Days of the Week [Numeric, 0-6]
| | | | |

          • home/path/to/command/the_command.sh
            因此,例如,如果您想将任务安排在每个月的第一天凌晨 12 点,它看起来像这样:

0 0 1 home/path/to/command/the_command.sh
另一方面,如果你想安排一个任务在每个星期六上午 8:30 运行,我们会这样写:

30 8 6 home/path/to/command/the_command.sh
还有许多运算符可用于进一步自定义时间表:

逗号用于为任何 cron 列创建逗号分隔的值列表。
短划线用于指定值范围。
星号用于指定所有或每个值。
默认情况下,crontab 会在执行计划任务时发送电子邮件通知。但是,在许多情况下,这是不需要的。我们可以通过将此命令的标准输出重定向到黑洞或设备来轻松抑制此功能。从本质上讲,这是一个文件,它将丢弃写入其中的所有内容。输出重定向通过运算符完成。让我们来看看如何使用我们的示例 cron 作业将标准输出重定向到黑洞,该作业在每个星期六上午 8:30 运行一个计划任务:/dev/null>

30 8 6 home/path/to/command/the_command.sh >/dev/null
此外,如果我们将标准输出重定向到 null 设备,我们可能还希望重定向标准错误。我们可以通过简单地将标准错误重定向到标准输出已经重定向到已经重定向的位置(空设备)来做到这一点!

30 8 6 home/path/to/command/the_command.sh >/dev/null 2>&1
使用 PHP 管理 crontab:蓝图
为了使用 PHP 管理 crontab,我们需要能够以我们正在编辑其 crontab 的用户的身份在远程服务器上执行命令。虽然您可以使用 PHP 提供的 SSH2 库,但我们将使用 phpseclib 库,它支持不同的协议与远程服务器进行通信。

如何安装和使用 phpseclib 库
phpseclib库(PHP Secure Communications Library)是一个PHP库,它为SSH,SFTP和SSL / TLS等安全通信协议提供了一组方法和类。它使我们能够将安全通信功能整合到我们的应用程序中,而无需依赖外部命令行工具或扩展。

如果你想知道为什么你更喜欢使用 phpseclib 而不是 PHP 提供的核心 SSH2 函数,让我们来看看 phpseclib 库的好处:

SSH2 库提供的功能有限,可能不支持所有 SSH 功能
提供抽象,使您可以轻松地在不同协议之间切换
抽象底层协议的复杂性
易于使用且直观
它不需要任何外部依赖项或扩展
支持多种安全通信协议,包括 SSH、SFTP、SCP、FTPS、SSL/TLS 等
有据可查、积极开发和社区支持
如您所见,phpseclib 库可以为安全通信需求提供更全面、更强大的解决方案。

让我们看看如何安装它。您可以使用 composer 安装 phpseclib 库,如以下代码片段所示。

$composer install phpseclib/phpseclib
安装后,您可以通过在 PHP 脚本顶部包含自动加载器脚本来开始在 PHP 代码中使用 phpseclib 库,如以下代码片段所示。

<?php
require 'vendor/autoload.php';
让我们快速浏览一下如何进行远程SSH2登录。

<?php
require 'vendor/autoload.php';
use phpseclib3\Net\SSH2;
$ssh = new SSH2('example.remote-server.com');
if (!$ssh->login('ssh-username', 'ssh-password')) {

die('Login to remote server failed.');

}
以上是对 phpseclib 库的简要介绍。

类模板Ssh2_crontab_manager
在本节中,我们将讨论需要在类中实现哪些方法。

我们的类必须能够以适当的用户身份进行连接和身份验证,以便执行命令并访问用户的 crontab。因此,我们将建立一个 SSH2 连接并在构造函数本身中对其进行身份验证。

有了经过身份验证的连接,我们需要一种方法来处理我们将运行的各种命令的执行。我们将使用 phpseclib 库提供的方法。通常,当我们需要在远程服务器上执行命令时,我们将从类的其他方法中调用此方法。exec()

接下来,我们需要能够将 crontab 写入文件,以便我们可以切实地访问它。我们还需要一种方法在完成此文件后将其删除。我们将定义将 crontab 输出到文件的方法定义为 ,并将删除此文件的方法定义为 。write_to_file()remove_file()

当然,我们还需要一种方法来创建和删除 cron 作业。因此,我们将分别定义和方法。append_cronjob()remove_cronjob()

如果我们删除了唯一或最后一个 cron 作业,我们应该可以选择删除整个 crontab,而不是尝试创建一个空的 crontab。为了处理这种情况,我们可以使用该方法。remove_crontab()

最后,我们将为类创建两个帮助程序方法。这些方法中的第一个将返回一个布尔值,它将简单地检查临时 cron 文件是否存在。后者将用于在发生错误时显示错误消息。我们将分别命名这些方法。crontab_file_exists()error_message()

我们的准系统PHP类应该看起来像这样:

<?php
use phpseclib3\Net\SSH2;
Class Ssh2_crontab_manager {

private $connection;
private $path;
private $handle;
private $cron_file;

public function __construct() {...}

public function exec() {...}

public function write_to_file() {...}

public function remove_file() {...}

public function append_cronjob() {...}

public function remove_cronjob() {...}

public function remove_crontab() {...}

private function crontab_file_exists() {...}

private function error_message() {...}

}
?>
实现类Ssh2_crontab_manager
在本节中,我们将实现类的各种方法。

构造函数
类构造函数将主要负责建立和验证 SSH2 连接。

让我们看一下方法。__construct

public function __construct($host = null, $port = null, $username = null, $password = null)
{

$path_length = strrpos(__FILE__, "/");
$this->path = substr(__FILE__, 0, $path_length) . '/';
$this->handle = 'crontab.txt';
$this->cron_file = "{$this->path}{$this->handle}";
try {
    if (is_null($host) || is_null($port) || is_null($username) || is_null($password)) {
        throw new Exception("Please specify the host, port, username, and password!");
    }
    $this->connection = new SSH2($host, $port);
    if (!$this->connection->login($username, $password)) {
        throw new Exception("Could not authenticate '{$username}' using password: '{$password}'.");
    }
} catch (Exception $e) {
    $this->error_message($e->getMessage());
}

}
首先,它需要四个参数,每个参数的默认值为:NULL

$host:表示我们要连接的远程服务器的 IP 地址。
$port:用于 SSH2 连接的端口。
$username:表示服务器的用户的登录名。
$password:表示用户对服务器的密码。
首先,它计算当前文件的路径并将其设置为变量。它还设置要使用的 cron 文件的名称,并在 中构造 cron 文件的完整路径。$this->path$this->handle$this->cron_file

然后,我们检查是否缺少任何必需的参数,如果是,它会抛出带有相应错误消息的异常。

接下来,它通过传入 and 值来创建类的实例以建立 SSH 连接。SSH2$host$port

最后,它尝试使用提供的 和 通过调用 SSH2 连接对象的方法进行身份验证。如果身份验证成功,则建立连接。如果失败,将引发异常。$username$passwordlogin()

方法exec
该方法负责在远程服务器上执行命令。这相当于手动将命令输入到像PuTTY这样的shell中。为了提供更大的灵活性,我们将公开此方法,以便用户可以实际执行他们可能需要运行的任何其他命令。exec

让我们看一下方法。exec

public function exec()
{

$argument_count = func_num_args();

try {
    if (!$argument_count) {
        throw new Exception("There is nothing to execute, no arguments specified.");
    }

    $arguments = func_get_args();
    $command_string = ($argument_count > 1) ? implode(" && ", $arguments) : $arguments[0];
    $stream = $this->connection->exec($command_string);

    if (!$stream) {
        throw new Exception("Unable to execute the specified commands: <br />{$command_string}");
    }
} catch (Exception $e) {
    $this->error_message($e->getMessage());
}

return $this;

}
我们的方法使用 phpseclib 库的方法执行命令。exec()exec

在此方法中,我们要做的第一件事是定义一个变量,表示传递的参数总数的计数。我们将使用 PHP 的函数来获取此计数。接下来,在 try 块中,我们将检查是否将任何参数传递给此方法。如果参数计数为 0,我们将抛出带有相应消息的新异常。func_num_args()

接下来,使用该函数,我们将创建一个包含传递给此方法的所有参数的数组。然后,使用三元运算符,我们将定义一个新变量 ,作为我们将要执行的实际 Linux 命令的单行字符串表示形式。func_get_args()$command_string

如果我们确实有多个命令要执行,我们将使用 PHP 的函数将参数数组解析为字符串。我们将两个参数传递给:胶水或分隔符,它基本上只是一个将插入数组元素之间的字符串,以及数组本身。我们将使用 Linux 运算符分隔每个元素,这允许我们在一行上按顺序执行多个命令!implode()implode()&&

准备好命令并将其解析为字符串后,我们现在可以尝试使用 SSH2 类中的方法通过已建立的 SSH 连接执行命令字符串。返回值存储在变量中。exec()$stream
方法write_to_file
下一个方法 , 将负责将现有 crontab 写入临时文件,或者在不存在 cron 作业时创建一个空白的临时文件。这将需要两个参数:write_to_file()

我们将创建的临时文件的路径
创建时应该使用的名称
该方法应如下所示。write_to_file

public function write_to_file($path=NULL, $handle=NULL)
{

if (! $this->crontab_file_exists())
{    
    $this->handle = (is_null($handle)) ? $this->handle : $handle;
    $this->path   = (is_null($path))   ? $this->path   : $path;
    $this->cron_file = "{$this->path}{$this->handle}";
    $init_cron = "crontab -l > {$this->cron_file} && [ -f {$this->cron_file} ] || > {$this->cron_file}";

    $this->exec($init_cron);
}

return $this;

}
首先,我们使用布尔方法检查 cron 文件是否已存在,我们稍后将创建该方法。如果文件存在,则无需继续。如果没有,我们将使用三元运算符来检查 和 props 以确定它们是否是 .如果它们中的任何一个是 ,我们将使用构造函数中的预定义回退来定义它们。crontab_file_exists()$path$handleNULLNULL

然后,我们将这些属性连接成一个新属性,该属性表示临时 cron 文件的完整路径和文件名。

接下来,我们使用带有参数的 Linux 命令将用户 crontab 显示为标准输出。然后,使用 Linux 的运算符,我们将标准输出或我们的临时 cron 文件重定向到我们的临时 cron 文件,方法是连接到命令字符串!使用运算符将输出重定向到文件将始终创建该文件(如果该文件不存在)。crontab-l>STDOUT$this->cron_file>

这很好用,但前提是 crontab 中已经安排了作业。如果没有 cron 作业,则永远不会创建此文件!但是,使用运算符,我们可以将其他命令/表达式附加到此字符串中。因此,让我们附加一个条件表达式来检查 cron 文件是否存在。如果该文件不存在,则此表达式的计算结果将为 false。因此,如果需要,我们可以在此条件之后使用 or 运算符创建一个新的空白文件。&&||or

如果条件计算结果为 false,则在之后放置的命令将执行。现在,通过再次使用运算符,这次没有在它前面加上特定的命令,我们可以创建一个新的空白文件。因此,从本质上讲,这串命令会将 crontab 输出到一个文件中,然后检查该文件是否存在,这将指示 crontab 中有条目,然后创建一个新的空白文件(如果它尚不存在)。or>

最后,我们调用了该方法并将命令字符串作为唯一的参数传递给它。然后,为了使此方法也可链接,我们将返回 .exec()$this

方法remove_file
让我们快速看一下该方法。remove_file

public function remove_file()
{

if ($this->crontab_file_exists()) $this->exec("rm {$this->cron_file}");

return $this;

}
在此方法中,我们使用辅助方法 , 来检查临时 cron 文件是否存在,然后执行 Linux 命令将其删除(如果存在)。像往常一样,我们还将返回以保持链条可伸缩性。crontab_file_exists()rm$this

方法append_cronjob
该方法通过将新的作业/行添加到临时 cron 文件,然后在该文件上执行命令来创建新的 cron 作业,这会将所有这些作业安装为新的 crontab。append_cronjobcrontab

它看起来像这样:

public function append_cronjob($cron_jobs=NULL)
{

if (is_null($cron_jobs)) $this->error_message("Nothing to append! Please specify a cron job or an array of cron jobs.");

$append_cronfile = "echo '";        

$append_cronfile .= (is_array($cron_jobs)) ? implode("\n", $cron_jobs) : $cron_jobs;

$append_cronfile .= "' >> {$this->cron_file}";

$install_cron = "crontab {$this->cron_file}";

$this->write_to_file()->exec($append_cronfile, $install_cron)->remove_file();

return $this;

}
该方法接受一个参数,该参数将是一个字符串或表示要追加的 cron 作业的字符串数组。append_cronjob()$cron_jobs

我们将通过确定参数是否为 .如果是,我们将调用该方法以停止任何进一步的执行,并向用户显示错误消息。$cron_jobsNULLerror_message()

接下来,我们将一个新变量 定义为字符串,文本后跟空格,末尾带有单引号。我们将暂时用我们正在添加的各种 cron 作业以及结束引号填充此字符串。我们将使用字符串连接运算符构建此字符串。$append_cronfileecho.=

接下来,使用三元运算符,我们确定是否是数组。如果是,我们将用换行符内爆该数组,以便每个 cron 作业都写在 cron 文件中自己的行上。如果参数不是数组,我们将简单地将该作业连接到字符串上,而无需任何特殊处理。$cron_jobs\n$cron_jobs$append_cron

本质上,我们可以通过再次将标准输出重定向到文件中来将我们的任务回显到 cron 文件中。因此,通过使用字符串连接运算符,我们将右单引号附加到命令字符串,以及 Linux 运算符,后跟 cron 文件的完整路径和文件名。与始终覆盖文件的运算符不同,运算符将输出追加到文件末尾。因此,通过使用此运算符,我们不会覆盖任何现有的 cron 作业。>>>>>

接下来,我们将一个变量定义为字符串,使用我们将用于安装我们将要创建的新 cron 文件的命令。这就像调用 crontab 命令一样简单,后跟 cron 文件的路径和文件名。

最后,在通过该方法执行这些命令之前,我们将首先调用该方法来创建临时 cron 文件。然后,在链中,我们将执行这些命令并调用该方法删除临时文件。最后,我们将返回,以便该方法可链接。exec()write_to_file()remove_file()$thisappend_cronjob()

方法remove_cronjob
现在我们可以创建新的 cron 作业,我们也有能力删除它们是合乎逻辑的!这就是该方法的目的。remove_cronjob

一起来看看吧。

public function remove_cronjob($cron_jobs=NULL)
{

if (is_null($cron_jobs)) $this->error_message("Nothing to remove! Please specify a cron job or an array of cron jobs.");

$this->write_to_file();

$cron_array = file($this->cron_file, FILE_IGNORE_NEW_LINES);

if (empty($cron_array)) $this->error_message("Nothing to remove! The cronTab is already empty.");

$original_count = count($cron_array);

if (is_array($cron_jobs))
{
    foreach ($cron_jobs as $cron_regex) $cron_array = preg_grep($cron_regex, $cron_array, PREG_GREP_INVERT);
}
else
{
    $cron_array = preg_grep($cron_jobs, $cron_array, PREG_GREP_INVERT);
}    

return ($original_count === count($cron_array)) ? $this->remove_file() : $this->remove_crontab()->append_cronjob($cron_array);

}
它需要一个参数,这将是一个(简单的)正则表达式。它将用于在 crontab 中查找匹配的作业并相应地删除它们。

创建 cron 文件后,我们现在将使用 PHP 的函数将其读入数组。此函数会将给定文件解析为数组,每行都作为数组元素。我们将 cron 文件作为第一个参数传递给此函数,然后设置一个特殊标志,这将强制忽略所有新行。因此,我们有一个只有 cron 作业本身的数组。如果没有计划任何 cron 作业,则此数组将为空。随后,将没有理由继续。因此,我们已经检查了 是否为空,如果是,则停止执行。file()FILE_IGNORE_NEW_LINESfile()$cron_array

现在,我们确定参数是否是数组。如果它是一个数组,我们用循环遍历它。在该循环中,我们执行一个 函数 .这个漂亮的函数,与 不同,将返回与指定的正则表达式匹配的所有数组元素的数组。但是,在这种情况下,我们需要不匹配的数组元素。换句话说,我们需要一个包含我们将要保留的所有 cron 作业的数组,以便我们可以只使用这些作业初始化 crontab。因此,我们将在这里设置一个特殊的标志,这将导致返回一个与正则表达式不匹配的所有元素的数组。因此,我们将拥有一系列我们想要保留的所有 cron 作业。$cron_jobsforeachpreg_grep()preg_match()PREG_GREP_INVERTpreg_grep()

如果参数不是数组,我们将以相同的方式进行,但没有任何迭代。同样,我们将重新定义为设置了标志的函数的结果数组。$cron_jobs$cron_arraypreg_grep()PREG_GREP_INVERT

使用我们的集合,现在我们将该数组的当前长度与其原始长度进行比较,我们将其缓存在变量中。如果长度相同,我们将简单地返回删除临时 cron 文件的方法。如果它们不匹配,我们将删除现有的 crontab,然后安装新的。$cron_array$original_countremove_file()

方法remove_crontab
删除整个 crontab 相对容易实现,如以下代码片段所示。

public function remove_crontab()
{

$this->exec("crontab -r")->remove_file();

return $this;

}
其他帮助程序方法
在编写了首当其冲的 cron 管理类之后,我们现在将看看我们在整个课程中使用的两个小而有用的方法,以及 .crontab_file_exists()error_message()

crontab_file_exists法
此方法返回 PHP 方法的结果,或者 ,具体取决于临时 cron 文件是否存在。file_exists()truefalse

private function crontab_file_exists()
{

return file_exists($this->cron_file);

}
error_message法
此方法采用单个参数(一个字符串)表示我们要显示的错误消息。然后,我们将调用 PHP 的方法停止执行并显示此消息。字符串本身将连接成一个应用简单样式的元素。die()

private function error_message($error)
{

die("<pre style='color:#EE2711'>ERROR: {$error}</pre>");

}
完整的类如下所示:

<?php
use phpseclib3\Net\SSH2;
Class Ssh2_crontab_manager {

private $connection;
private $path;
private $handle;
private $cron_file;

public function __construct($host = null, $port = null, $username = null, $password = null)
{
    $path_length = strrpos(__FILE__, "/");
    $this->path = substr(__FILE__, 0, $path_length) . '/';
    $this->handle = 'crontab.txt';
    $this->cron_file = "{$this->path}{$this->handle}";
    
    try {
        if (is_null($host) || is_null($port) || is_null($username) || is_null($password)) {
            throw new Exception("Please specify the host, port, username, and password!");
        }
        
        $this->connection = new SSH2($host, $port);
        if (!$this->connection->login($username, $password)) {
            throw new Exception("Could not authenticate '{$username}' using password: '{$password}'.");
        }
    } catch (Exception $e) {
        $this->error_message($e->getMessage());
    }
}

public function exec()
{
    $argument_count = func_num_args();
    
    try {
        if (!$argument_count) {
            throw new Exception("There is nothing to execute, no arguments specified.");
        }
        
        $arguments = func_get_args();
        $command_string = ($argument_count > 1) ? implode(" && ", $arguments) : $arguments[0];
        $stream = $this->connection->exec($command_string);
        
        if (!$stream) {
            throw new Exception("Unable to execute the specified commands: <br />{$command_string}");
        }
    } catch (Exception $e) {
        $this->error_message($e->getMessage());
    }
    
    return $this;
}

public function write_to_file($path=NULL, $handle=NULL)
{
    if (! $this->crontab_file_exists())
    {    
        $this->handle = (is_null($handle)) ? $this->handle : $handle;
        $this->path   = (is_null($path))   ? $this->path   : $path;
        $this->cron_file = "{$this->path}{$this->handle}";
        $init_cron = "crontab -l > {$this->cron_file} && [ -f {$this->cron_file} ] || > {$this->cron_file}";
        
        $this->exec($init_cron);
    }
    
    return $this;
}

public function remove_file()
{
    if ($this->crontab_file_exists()) $this->exec("rm {$this->cron_file}");
    
    return $this;
}

public function append_cronjob($cron_jobs=NULL)
{
    if (is_null($cron_jobs)) $this->error_message("Nothing to append! Please specify a cron job or an array of cron jobs.");
    
    $append_cronfile = "echo '";        
    
    $append_cronfile .= (is_array($cron_jobs)) ? implode("\n", $cron_jobs) : $cron_jobs;
    
    $append_cronfile .= "' >> {$this->cron_file}";
    
    $install_cron = "crontab {$this->cron_file}";
    
    $this->write_to_file()->exec($append_cronfile, $install_cron)->remove_file();
    
    return $this;
}

public function remove_cronjob($cron_jobs=NULL)
{
    if (is_null($cron_jobs)) $this->error_message("Nothing to remove! Please specify a cron job or an array of cron jobs.");
    
    $this->write_to_file();
    
    $cron_array = file($this->cron_file, FILE_IGNORE_NEW_LINES);
    
    if (empty($cron_array)) $this->error_message("Nothing to remove! The cronTab is already empty.");
    
    $original_count = count($cron_array);
    
    if (is_array($cron_jobs))
    {
        foreach ($cron_jobs as $cron_regex) $cron_array = preg_grep($cron_regex, $cron_array, PREG_GREP_INVERT);
    }
    else
    {
        $cron_array = preg_grep($cron_jobs, $cron_array, PREG_GREP_INVERT);
    }    
    
    return ($original_count === count($cron_array)) ? $this->remove_file() : $this->remove_crontab()->append_cronjob($cron_array);
}

public function remove_crontab()
{
    $this->exec("crontab -r")->remove_file();
    
    return $this;
}

private function crontab_file_exists()
{
    return file_exists($this->cron_file);
}

private function error_message($error)
{
    die("<pre style='color:#EE2711'>ERROR: {$error}</pre>");
}

}
?>
Putting It All Together
Now that we've completed our cron management class, let's take a look at a few examples of how to use it.

Instantiating the Class and Establishing an Authenticated Connection
Firstly, let's create a new instance of our class. Remember, we'll need to pass the IP address, port, username, and password to the class constructor.

<?php
require 'vendor/autoload.php';
include "./ssh2_crontab_manager.php";
$crontab = new Ssh2_crontab_manager('11.11.111.111', '22', 'my_username', 'my_password');
Appending a Single Cron Job
With an authenticated connection in place, let's have a look at how we can create a new, single, cron job.

<?php
require 'vendor/autoload.php';
include "./ssh2_crontab_manager.php";
$crontab = new Ssh2_crontab_manager('11.11.111.111', '22', 'my_username', 'my_password');
$crontab->append_cronjob('30 8 6 home/path/to/command/the_command.sh >/dev/null 2>&1');
Appending an Array of Cron Jobs
Appending multiple cron jobs is just as easy as appending a single cron job. We'll simply pass an array to the method.append_cronjob()

<?php
require 'vendor/autoload.php';
include "./ssh2_crontab_manager.php";
$crontab = new Ssh2_crontab_manager('11.11.111.111', '22', 'my_username', 'my_password');
$new_cronjobs = array(

'0 0 1 * * home/path/to/command/the_command.sh',
'30 8 * * 6 home/path/to/command/the_command.sh >/dev/null 2>&1'

);
$crontab->append_cronjob($new_cronjobs);
Removing a Single Cron Job
与创建单个 cron 作业的方式类似,我们现在将删除一个。但是,这一次,我们将使用正则表达式来查找适当的任务。此正则表达式可以像您需要的那样简单或复杂。实际上,有许多方法可以为您正在寻找的任务进行正则表达式。例如,如果您需要删除的任务是唯一的,因为正在运行的命令未在 crontab 中的其他任何地方使用,则只需将命令名称指定为正则表达式即可。此外,如果要删除某个月的所有任务,只需编写一个正则表达式即可查找给定月份所有作业的匹配项。

<?php
require 'vendor/autoload.php';
include "./ssh2_crontab_manager.php";
$crontab = new Ssh2_crontab_manager('11.11.111.111', '22', 'my_username', 'my_password');
$cron_regex = '/home/path/to/command/the_command.sh/';
$crontab->remove_cronjob($cron_regex);
删除 cron 作业数组
删除多个 cronjob 的处理方式与删除单个 cronjob 的方式相同,但有一个例外:我们将一个 cron job 正则表达式数组传递给该方法。remove_cronjob()

<?php
require 'vendor/autoload.php';
include "./ssh2_crontab_manager.php";
$crontab = new Ssh2_crontab_manager('11.11.111.111', '22', 'my_username', 'my_password');
$cron_regex = array(

'/0 0 1 \* \*/',
'/home\/path\/to\/command\/the_command\.sh\/'

);
$crontab->remove_cronjob($cron_regex);

0

Deprecated: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in /www/wwwroot/testblog.58heshihu.com/var/Widget/Archive.php on line 1032

评论 (0)

取消