GithubHelp home page GithubHelp logo

crontab's Introduction

Crontab

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.

Requirements

Should work on monst Linux flavoured systems.

Usage Example

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();

Methods

append($command)

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();

remove($command)

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();

getJobs()

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();

removeByKey($key)

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();

execute()

Applies and writes the new cronjob list.

    $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

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

Settings

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);

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.

    $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.

Todo

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

Disclaimer

Use this library at your own risk.

crontab's People

Contributors

devsdmf avatar

Watchers

Mark.Wang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.