php管理crontab

经常遇到计划任务,琢磨着用php或python写一个web管理界面,结果早就有先驱实现了,在此mark一下。

Jobby

简介

Jobby是一个PHP计划任务管理器,只需要安装好Jobby,便能管理你的离线计划任务,无需编辑系统的crontab,便能增加新的计划任务,Jobby能够记录、锁定、错误信息发送至邮件等,That is cool!

安装使用

1.安装composer;

2.composer.json中添加下面的依赖:

'hellogerard/jobby': 'dev-master'

3.执行命令进行安装:

composer install

4.系统crontab中添加以下任务:

* * * * * cd /path/to/project && php jobby.php 1>> /dev/null 2>&1

5.安装完Jobby之后,将示例二进制文件拷贝到项目根目录下即可:

$ cp vendor/hellogerard/jobby/resources/jobby.php .

示例代码

<?php
require(__DIR__ . '/vendor/autoload.php');
$jobby = new \Jobby\Jobby();
// Every job has a name
$jobby->add('CommandExample', array(
 // Commands are either shell commands or anonymous functions
 'command' => 'ls',
// Ordinary crontab schedule format is supported. This schedule runs every
 // hour
 'schedule' => '0 * * * *',
// Stdout and stderr is sent to the specified file
 'output' => 'logs/command.log',
// You can turn off a job by setting 'enabled' to false
 'enabled' => true,
));
$jobby->add('ClosureExample', array(
 // Commands can be PHP closures
 'command' => function() {
 echo "I'm a function!\n";
 return true;
 },
// This function will run every other hour
 'schedule' => '0 */2 * * *',
'output' => 'logs/closure.log',
 'enabled' => true,
));
$jobby->run();

php-crontab-manager

简介

php-crontab-manager管理linux下计划任务,新增、编辑、删除等操作都是在安全模式下进行的,即不使用”crontab -e”对系统文件进行编辑,而是对临时文件进行操作。

安装使用

1.拷贝源文件至项目文件夹中;

2.引入文件即可;

示例代码

<?php
use php\manager\crontab\CrontabManager;
$crontab = new CrontabManager();
$job = $crontab->newJob();
$job->on('* * * * *');
$job->onMinute('20-30')->doJob("echo foo");
$crontab->add($job);
$job->onMinute('35-40')->doJob("echo bar");
$crontab->add($job);
$crontab->save();

Crontab Class

crontab class是利用php的程序执行函数shell_exec()进行计划任务的增删改查。

源代码

代码很简单明了

class Crontab {
 
 // In this class, array instead of string would be the standard input / output format.
 
 // Legacy way to add a job:
 // $output = shell_exec('(crontab -l; echo "'.$job.'") | crontab -');
 
 static private function stringToArray($jobs = '') {
 $array = explode("\r\n", trim($jobs)); // trim() gets rid of the last \r\n
 foreach ($array as $key => $item) {
 if ($item == '') {
 unset($array[$key]);
 }
 }
 return $array;
 }
 
 static private function arrayToString($jobs = array()) {
 $string = implode("\r\n", $jobs);
 return $string;
 }
 
 static public function getJobs() {
 $output = shell_exec('crontab -l');
 return self::stringToArray($output);
 }
 
 static public function saveJobs($jobs = array()) {
 $output = shell_exec('echo "'.self::arrayToString($jobs).'" | crontab -');
 return $output; 
 }
 
 static public function doesJobExist($job = '') {
 $jobs = self::getJobs();
 if (in_array($job, $jobs)) {
 return true;
 } else {
 return false;
 }
 }
 
 static public function addJob($job = '') {
 if (self::doesJobExist($job)) {
 return false;
 } else {
 $jobs = self::getJobs();
 $jobs[] = $job;
 return self::saveJobs($jobs);
 }
 }
 
 static public function removeJob($job = '') {
 if (self::doesJobExist($job)) {
 $jobs = self::getJobs();
 unset($jobs[array_search($job, $jobs)]);
 return self::saveJobs($jobs);
 } else {
 return false;
 }
 }
 
}