Simple, independant and local PHP Crontab package
The package helps you manage your local cron jobs using PHP. You can list, append and remove your jobs and set your log file.
Should work on monst Linux flavoured systems.
Use composer to install it or simply include the file somewhere: require("crontab/src/Crontab/Crontab.php");
Append two new jobs and set them to run every minute:
$cron = new \Crontab\Crontab();
$cron->setMinute("*");
$cron->setHour("*");
$cron->setDayOfMonth("*");
$cron->setMonth("*");
$cron->setDayOfWeek("*");
$cron->append(array(
"date",
"ls -all"
)
);
$commandsList = $cron->execute();
Appends a new job to the current cronjob list
Parameters: $command : String or array of commands.
$cron = new \Crontab\Crontab();
$cron->setMinute("*");
$cron->setHour("*");
$cron->setDayOfMonth("*");
$cron->setMonth("*");
$cron->setDayOfWeek("*");
$cron->append("date");
$cron->execute();
Removes a job from the current cronjob list. You must recreate the exact job to remove it.
Parameters: $command : String or array of commands
$cron = new \Crontab\Crontab();
$cron->setMinute("*");
$cron->setHour("*");
$cron->setDayOfMonth("*");
$cron->setMonth("*");
$cron->setDayOfWeek("*");
$cron->remove("date");
$cron->execute();
Return a current list of jobs with there hashed keys
$cron = new \Crontab\Crontab();
$cron->setMinute("*");
$cron->setHour("*");
$cron->setDayOfMonth("*");
$cron->setMonth("*");
$cron->setDayOfWeek("*");
$cron->remove("date");
$cron->execute();
Removes a job from the current cronjob list by a hash key. Found by running execute() or getJobs()
Parameters: $key : String or array of keys
$cron = new \Crontab\Crontab();
$cron->removeByKey("1231231231231231231");
$cron->execute();
Applies and writes the new cronjob list.
$cron->setMinute("*");
$cron->setHour("*");
$cron->setDayOfMonth("*");
$cron->setMonth("*");
$cron->setDayOfWeek("*");
$cron->append("date");
$cron->execute();
Simply removes all running jobs by executing crontab -r
$cron = new \Crontab\Crontab();
$cron->clear();
Settings can also be applied to the constuct method like so:
$conf = array(
'minute' => '*',
'hour' => '*',
'dayOfMonth' => '*',
'month' => '*',
'dayOfWeek' => '*',
'logFile' => 'log.txt',
'tmpFile' => 'jobs.txt'
);
$cron = new \Crontab\Crontab($conf);
setMinute($m) : Sets the minute.
setHour($h) : Sets the hour.
setDayOfMonth($dom) : Sets the date of the month.
setMonth($m) : Sets the month.
setDayOfWeek($dow) : Sets the day of the week.
setLogFile($v) : Sets the log file and will attempt to create it. The default is /dev/null ie: nothing logged
setTmpFile($v) : Sets the tempory file used by crontab to read from, this file is automatically removed. The default is "jobs.txt"
Execute this job in 5 minutes from now.
$cron = new \Crontab\Crontab();
$cron->minuteFromNow(5);
$cron->execute();
A Google search should provied plenty of links but check out: Kevin van Zonneveld's blog if you need help.
- Email instead of log or both.
- Build in more heler functions.
- Unit tests
Use this library at your own risk.