Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@yushine
Forked from gustavgenberg/Crontab.class.php
Created June 19, 2018 07:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yushine/6699d3568e8a5597d918e946d1d18b96 to your computer and use it in GitHub Desktop.
Save yushine/6699d3568e8a5597d918e946d1d18b96 to your computer and use it in GitHub Desktop.
Easy crontab manager for PHP. Uses shell_exec!
<?php
class Crontab {
/*
(c) Gustav Genberg 2017
This script uses PHP shell_exec! Make sure it is enabled before using!
Also make sure that the user running this script (usually www-data) have access to the crontab command!
Note that this script should be working fine as it is (Apache2)
Crontab usage: crontab [-u user] file
crontab [ -u user ] [ -i ] { -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)
PHP-Crontab usage: Crontab::[ Read ] [ Set ] [ Add ] [ Remove ] [ Check ]
Read (Returns array of ALL [usually www-data] 's cronjobs)
Set (Takes array with cronjobs and REPLACES the existing cronjobs)
Add (Takes string formatted as a cronjob and adds it)
Remove (Takes string and will remove all cronjobs that matches the provided string [case-sensitive])
Check (Returns boolean based on cronjob existance)
Examples:
Crontab::Add('0 0 * * 6 sh /backup/run.sh');
Crontab::Remove('0 0 * * 6 sh /backup/run.sh');
// OR
Crontab::Remove('/backup/run.sh');
Crontab::Check('/backup/run.sh'); // false
*/
public static function Read () {
$Jobs = shell_exec('crontab -l');
$Jobs = explode("\n", $Jobs);
array_pop($Jobs);
return $Jobs;
}
public static function Set ($Jobs) {
$file = fopen('/tmp/php-crontab.tmp', 'w');
for($i = 0; $i < count($Jobs); $i++) {
fwrite($file, $Jobs[$i] . "\n");
}
fclose($file);
shell_exec('crontab /tmp/php-crontab.tmp');
unlink('/tmp/php-crontab.tmp');
}
public static function Add ($Job) {
$Jobs = Crontab::Read();
if(Crontab::Check($Job)) return;
array_push($Jobs, $Job);
Crontab::Set($Jobs);
}
public static function Remove ($Job) {
$Jobs = Crontab::Read();
$UpdatedJobs = [];
for($i = 0; $i < count($Jobs); $i++) {
if(strpos($Jobs[$i], $Job) !== false) continue;
array_push($UpdatedJobs, $Jobs[$i]);
}
Crontab::Set($UpdatedJobs);
}
public static function Check ($Job) {
$Jobs = Crontab::Read();
for($i = 0; $i < count($Jobs); $i++) {
if(strpos($Jobs[$i], $Job) !== false) return true;
}
return false;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment