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

by Yang Yang on October 30, 2011

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.

Subscribe to Kavoir: blog feed

You should also read:

Karl November 4, 2012 at 5:37 am

This works fantastic on hostgator (VPS)!

Yang Yang December 22, 2012 at 5:40 pm

Glad it works! :)

DieFeM December 21, 2012 at 6:12 am

Thanks, your PHP class is awesome, i used it in my crontab module for OpenGamePanel:
http://picbit.net/image/20122012_1356020839767_1356000717755.jpg

Yang Yang December 22, 2012 at 5:39 pm

Please make a backlink back to this page. It would be greatly appreciated!

Nais Ajiz December 28, 2012 at 4:49 pm

Amazing….it works at my site. Thanks A lot…

Kurt February 28, 2013 at 4:03 am

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?

ronaldo March 23, 2013 at 12:44 am

this class very good, simple, straightforward and functional.

(Brasil)

Dav March 27, 2013 at 10:05 pm

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

Comments on this entry are closed.

{ 2 trackbacks }

Previous post:

Next post: