2017 © Pedro Peláez
 

library crontab

PHP Cronjob manager

image

egersdorfer/crontab

PHP Cronjob manager

  • Monday, December 22, 2014
  • by CodeChap
  • Repository
  • 2 Watchers
  • 14 Stars
  • 102 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 14 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Crontab

Simple, independant and local PHP Crontab package, (*1)

The package helps you manage your local cron jobs using PHP. You can list, append and remove your jobs and set your log file., (*2)

Requirements

Should work on monst Linux flavoured systems., (*3)

Usage Example

Use composer to install it or simply include the file somewhere: require("crontab/src/Crontab/Crontab.php");, (*4)

Append two new jobs and set them to run every minute:, (*5)

    $cron = new \Crontab\Crontab();
    $cron->setMinute("*");
    $cron->setHour("*");
    $cron->setDayOfMonth("*");
    $cron->setMonth("*");
    $cron->setDayOfWeek("*");
    $cron->append(array(
                "date",
                "ls -all"
            )
        );
    $commandsList = $cron->execute();

Methods

append($command)

Appends a new job to the current cronjob list, (*6)

Parameters: $command : String or array of commands., (*7)

  $cron = new \Crontab\Crontab();
  $cron->setMinute("*");
  $cron->setHour("*");
  $cron->setDayOfMonth("*");
  $cron->setMonth("*");
  $cron->setDayOfWeek("*");
  $cron->append("date");
  $cron->execute();

remove($command)

Removes a job from the current cronjob list. You must recreate the exact job to remove it., (*8)

Parameters: $command : String or array of commands, (*9)

  $cron = new \Crontab\Crontab();
  $cron->setMinute("*");
  $cron->setHour("*");
  $cron->setDayOfMonth("*");
  $cron->setMonth("*");
  $cron->setDayOfWeek("*");
  $cron->remove("date");
  $cron->execute();

getJobs()

Return a current list of jobs with there hashed keys, (*10)

  $cron = new \Crontab\Crontab();
  $cron->setMinute("*");
  $cron->setHour("*");
  $cron->setDayOfMonth("*");
  $cron->setMonth("*");
  $cron->setDayOfWeek("*");
  $cron->remove("date");
  $cron->execute();

removeByKey($key)

Removes a job from the current cronjob list by a hash key. Found by running execute() or getJobs(), (*11)

Parameters: $key : String or array of keys, (*12)

  $cron = new \Crontab\Crontab();
  $cron->removeByKey("1231231231231231231");
  $cron->execute();

execute()

Applies and writes the new cronjob list., (*13)

    $cron->setMinute("*");
    $cron->setHour("*");
    $cron->setDayOfMonth("*");
    $cron->setMonth("*");
    $cron->setDayOfWeek("*");
    $cron->append("date");
    $cron->execute();

clear()

Simply removes all running jobs by executing crontab -r, (*14)

    $cron = new \Crontab\Crontab();
    $cron->clear();

Settings

Settings can also be applied to the constuct method like so:, (*15)

    $conf = array(
        'minute' => '*',
        'hour' => '*',
        'dayOfMonth' => '*',
        'month' => '*',
        'dayOfWeek' => '*',
        'logFile' => 'log.txt',
        'tmpFile' => 'jobs.txt'
    );

    $cron = new \Crontab\Crontab($conf);

Setting functions (Defaults are all "*" here)

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.

Log and tempory file settings

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"

Helpers

Execute this job in 5 minutes from now., (*16)

    $cron = new \Crontab\Crontab();
    $cron->minuteFromNow(5);
    $cron->execute();

Help understanding cronjobs

A Google search should provied plenty of links but check out: Kevin van Zonneveld's blog if you need help., (*17)

Todo

  • Email instead of log or both.
  • Build in more heler functions.
  • Unit tests

Disclaimer

Use this library at your own risk., (*18)

The Versions

22/12 2014

dev-develop

dev-develop https://github.com/codeChap/crontab

PHP Cronjob manager

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Derrick Egersdorfer

cronjob cron crontab