skip to Main Content

I want to add cron job dynamically once user install a php application on their server, like admin configuration, I need to set cron job dynamically once going thought its configuration settings in php?
I am using codeigniter to set the cron job, also I added the same from cpanel manually and it is working fine.

4

Answers


  1. Chosen as BEST ANSWER

    Hi thank you for the replies, i got my answers now, based on the replies I tried to add a new cron job to crontab file using php (codeigniter): following is my answer

    $phppath =  exec('which php');
    $user_file_path =  getcwd();
    $cronjob1 = "0 0 1 * * $phppath  $user_file_path/index.php 
    automatic_updates/leave_update_cron";
    // run each cron job
    // get all current cron jobs
    $output = shell_exec('crontab -l');
    // add our new job
    file_put_contents('/tmp/crontab.txt', $output.$cronjob1.PHP_EOL);
    // once append the job, execute the new file
    exec('crontab /tmp/crontab.txt');
    

    This will add a new cron job without deleting anything on the current cron job file


  2. You can create a file in

    /etc/cron.d

    and use PHP’s file_put_contents() to update the file. However, you’ll need to elevate PHP or Apache’s permissions (depending on your PHP handler). This isn’t recommended since it leads to security issues.

    If you’re using Cron to run PHP scripts, then you can call the scripts directly using PHP every X amount of time. Create a variable in your database representing the last time your script was run. If X amount of time passed since the script was run, then you run the script and update the variable.
    If the script takes a long time to execute, then use PHP to run it in a separate process, so the user doesn’t have to wait for it to finish.

    Thanks

    Login or Signup to reply.
  3. you can do this using shell script

    shell_exec('echo "25 15 * * * <path to php> /var/www/cronjob/helloworld.php > /var/www/cronjob/cron.log" | crontab -')
    

    you can use these functions or class

    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("rn", trim($jobs)); // trim() gets rid of the last rn
    foreach ($array as $key => $item) {
        if ($item == '') {
            unset($array[$key]);
        }
    }
    return $array;
    }
    
    static private function arrayToString($jobs = array()) {
        $string = implode("rn", $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;
          }
      }
     }
    
    Login or Signup to reply.
  4. First You have to create one txt file for example crontab.txt
    then you have to use shell script like below

    exec ( 'sudo crontab -u apache -r' );
    file_put_contents ( '/var/www/html/YOUR_PROJECT/crontab.txt',"25 15 * * * php /var/www/html/YOUR_PROJECT/YOUR_CONTROLLER/YOUR_METHOD'.PHP_EOL);
    

    And Lastly, you have to execute that file like

    exec ( 'crontab /var/www/html/YOUR_PROJECT/crontab.txt' );
    

    Hope this will help you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search