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

Provided that your user account on the server has the privileges to access crontab thus can create or remove cron jobs, you can use this PHP class to integrate crontab in your application. I created it for many of my own projects that need crontab to do scheduled jobs. It’s pretty straightforward.

Don’t know what crontab is and how it works? Read here and here. With this class, you or your users can easily set up crontab jobs and automate tasks by schedule with the web interface.

The Crontab 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("\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;
		}
	}
	
}

Public Methods

You may well ignore the private methods that do the internal chores. And keep in mind that any cron job is a text string.

  1. getJobs() — returns an array of existing / current cron jobs. Each array item is a string (cron job).
  2. saveJobs($jobs = array()) — save the $jobs array of cron jobs into the crontab so they would be run by the server. All existing jobs in crontab are erased and replaced by $jobs.
  3. doesJobExist($job = ”) — check if a specific job exist in crontab.
  4. addJob($job = ”) — add a cron job to the crontab.
  5. removeJob($job = ”) — remove a cron job from crontab.

This class has been tested on Rackspace Cloud and Wiredtree. Feel free to post your comments below and let me know how it works on other web hosts.

11 thoughts on “PHP: Crontab Class to Add, Edit and Remove Cron Jobs”

  1. Pingback: Resource Usage Cron Jobs Commands - Webmaster Forum

  2. Pingback: PHP: Crontab Class to Add, Edit and Remove Cron Jobs | prosoxi.com

  3. I tried it, but I encounter problems.

    I run job A which has to add a new job to the crontab.
    In this job A, I fetch the array of jobs, then add a new job B to the array, then call the saveJobs to push my changes to the crontab, it works OK.
    When I check afterwards with crontab -e, I see the new job at the bottom of the job list.

    Problem is, that the second time I run my job A, it returns an error “could not open input file”. That’s not true, because I just ran it one minute ago with success?
    So it looks like adding a new job B, somehow corrupts the crontab and makes it impossible to run job A afterwards.

    Any idea what can be the reason of this?

  4. Is it possible to be some permission issues ?
    Because as I run getJobs function it returns empty, though from command line ‘crontab -l’ returns the list of the jobs as expected. I use lampp server on linux

    Thanks

  5. Pingback: php管理crontab – 朱君无

Comments are closed.

Scroll to Top