PHP Crontab 管理:创建、编辑和删除作业
PHP 提供了管理 crontab 作业的功能,包括创建、编辑和删除作业删除。 Crontab 是一个功能强大的实用程序,可以按特定时间间隔在系统上安排任务。
创建 Crontab 作业
要使用 PHP 创建 crontab 作业,您可以利用shell_exec 函数:
$command = 'crontab -l'; $output = shell_exec($command); $job = '0 */2 * * * /usr/bin/php5 /home/user1/work.php'; $output .= "\n" . $job; $command = 'crontab'; shell_exec($command . ' /tmp/crontab.txt');
在此脚本中:
编辑 Crontab 作业
编辑 crontab 作业遵循类似的方法:
$command = 'crontab -l'; $output = shell_exec($command); $job = '0 5 * * * /usr/bin/php5 /home/user1/updated_work.php'; $output = str_replace('0 */2 * * * /usr/bin/php5 /home/user1/work.php', $job, $output); $command = 'crontab'; shell_exec($command . ' /tmp/crontab.txt');
通过更新 $output 的内容并将其传递给 crontab 来进行更改。
删除 Crontab 作业
删除 crontab 作业:
$command = 'crontab -r'; shell_exec($command);
此命令删除当前用户的所有 crontab 条目。要删除特定作业,需要手动编辑crontab文件并删除相应行。
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3