Software Developer

Delete a Single Cron Job from crontab With PHP

This is a quick PHP script to delete only a single cron job. This does not remove the whole crontab. You specify which cron command to remove. Just replace line 1 with your own cron command that you would like to delete.

(See instead how to create a cron job with PHP.)

PHP

$job = '0 * * * * php /home1/myypath/etc/mycron.php >/dev/null 2>&1';

// Remove a single cron job

exec('crontab -l', $data);

$key = array_search($job, $data);

if($key !== false){
    
    // the job was found, so remove it
  	unset($data[$key]);
  	
    // put the data back into the crontab
	exec('echo "'.implode("\n", $data).'" | crontab -');
}

By

Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this:

[code]

YOUR CODE HERE 

[/code]