是否可以使用PHP来创建,编辑和删除crontab作业?
我知道如何列出Apache用户的当前crontab作业:
$output = shell_exec('crontab -l');
echo $output;
Run Code Online (Sandbox Code Playgroud)
但是如何用PHP添加cron作业?'crontab -e'只会打开文本编辑器,您必须在保存文件之前手动编辑条目.
以及如何使用PHP删除cron作业?再次,您必须通过'crontab -e'手动执行此操作.
使用这样的作业字符串:
$job = '0 */2 * * * /usr/bin/php5 /home/user1/work.php';
Run Code Online (Sandbox Code Playgroud)
如何使用PHP将其添加到crontab作业列表?
ajr*_*eal 130
crontab命令用法
usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
Run Code Online (Sandbox Code Playgroud)
所以,
$output = shell_exec('crontab -l');
file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
echo exec('crontab /tmp/crontab.txt');
Run Code Online (Sandbox Code Playgroud)
如果用户具有足够的文件写入权限,则上述内容可用于创建和编辑/追加.
要删除作业:
echo exec('crontab -r');
Run Code Online (Sandbox Code Playgroud)
另外,请注意apache作为特定用户运行,并且通常不是root用户,这意味着除非为apache用户提供crontab -u特权,否则只能为apache用户更改cron作业.
Chr*_*ski 20
我们最近准备了一个迷你项目(PHP> = 5.3)来管理私人和个人任务的cron文件.此工具连接和管理cron文件,以便您可以使用它们,例如每个项目.单元测试可用:-)
命令行示例:
bin/cronman --enable /var/www/myproject/.cronfile --user www-data
Run Code Online (Sandbox Code Playgroud)
来自API的示例:
use php\manager\crontab\CrontabManager;
$crontab = new CrontabManager();
$crontab->enableOrUpdate('/tmp/my/crontab.txt');
$crontab->save();
Run Code Online (Sandbox Code Playgroud)
从API管理个人任务:
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();
Run Code Online (Sandbox Code Playgroud)
github:php-crontab-manager
Raf*_*shi 10
检查一个cronjob
function cronjob_exists($command){
$cronjob_exists=false;
exec('crontab -l', $crontab);
if(isset($crontab)&&is_array($crontab)){
$crontab = array_flip($crontab);
if(isset($crontab[$command])){
$cronjob_exists=true;
}
}
return $cronjob_exists;
}
Run Code Online (Sandbox Code Playgroud)
附加一个cronjob
function append_cronjob($command){
if(is_string($command)&&!empty($command)&&cronjob_exists($command)===FALSE){
//add job to crontab
exec('echo -e "`crontab -l`\n'.$command.'" | crontab -', $output);
}
return $output;
}
Run Code Online (Sandbox Code Playgroud)
删除crontab
exec('crontab -r', $crontab);
Run Code Online (Sandbox Code Playgroud)
例
exec('crontab -r', $crontab);
append_cronjob('* * * * * curl -s http://localhost/cron/test1.php');
append_cronjob('* * * * * curl -s http://localhost/cron/test2.php');
append_cronjob('* * * * * curl -s http://localhost/cron/test3.php');
Run Code Online (Sandbox Code Playgroud)
小智 5
这应该做到这一点
shell_exec("crontab -l | { cat; echo '*/1 * * * * command'; } |crontab -");
Run Code Online (Sandbox Code Playgroud)
我尝试了下面的解决方案
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;
}
}
Run Code Online (Sandbox Code Playgroud)
}