Use PHP to create, edit and delete crontab jobs?

前端 未结 12 1312
余生分开走
余生分开走 2020-11-22 06:54

Is it possible to use PHP to create, edit and delete crontab jobs?

I know how to list the current crontab jobs of the Apache user:

$output = shell_ex         


        
相关标签:
12条回答
  • 2020-11-22 07:09

    crontab command usage

    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)
    

    So,

    $output = shell_exec('crontab -l');
    file_put_contents('/tmp/crontab.txt', $output.'* * * * * NEW_CRON'.PHP_EOL);
    echo exec('crontab /tmp/crontab.txt');
    

    The above can be used for both create and edit/append provided the user has the adequate file write permission.

    To delete jobs:

    echo exec('crontab -r');
    

    Also, take note that apache is running as a particular user and that's usually not root, which means the cron jobs can only be changed for the apache user unless given crontab -u privilege to the apache user.

    0 讨论(0)
  • 2020-11-22 07:10

    You could try overriding the EDITOR environment variable with something like ed which can take a sequence of edit commands over standard input.

    0 讨论(0)
  • 2020-11-22 07:12

    Check a 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;
    }
    

    Append a 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;
    }
    

    Remove a crontab

    exec('crontab -r', $crontab);
    

    Example

    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');
    
    0 讨论(0)
  • 2020-11-22 07:16

    This should do it

    shell_exec("crontab -l | { cat; echo '*/1    *    *    *    *    command'; } |crontab -");
    
    0 讨论(0)
  • 2020-11-22 07:17

    Instead of crontab you could also use google's app engine task queue. It has a generous free quota, is fast, scalable, modifiable.

    0 讨论(0)
  • 2020-11-22 07:23

    I tried the solution below

    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;
        }
    }
    

    }

    credits to : Crontab Class to Add, Edit and Remove Cron Jobs

    0 讨论(0)
提交回复
热议问题