Home

Awesome

Jobby, a PHP cron job manager

Total Downloads Latest Version Build Status MIT License

Install the master jobby cron job, and it will manage all your offline tasks. Add jobs without modifying crontab. Jobby can handle logging, locking, error emails and more.

NEW REPO: We have moved jobby to a Github org. Please update your remotes to https://github.com/jobbyphp/jobby.git.

Features

Getting Started

Installation

The recommended way to install Jobby is through Composer:

$ composer require hellogerard/jobby

Then add the following line to your (or whomever's) crontab:

* * * * * cd /path/to/project && php jobby.php 1>> /dev/null 2>&1

After Jobby installs, you can copy an example file to the project root.

$ cp vendor/hellogerard/jobby/resources/jobby.php .

Running a job

<?php 

// Ensure you have included composer's autoloader  
require_once __DIR__ . '/vendor/autoload.php';

// Create a new instance of Jobby
$jobby = new Jobby\Jobby();

// Every job has a name
$jobby->add('CommandExample', [

    // Run a shell command
    'command'  => 'ls',

    // Ordinary crontab schedule format is supported.
    // This schedule runs every hour.
    'schedule' => '0 * * * *',

]);

$jobby->run();

Examples

Logging

<?php

/* ... */

$jobby->add('LoggingExample', [
    
    'command' => 'ls',
    'schedule' => '0 * * * *',
    
    // Stdout and stderr is sent to the specified file
    'output' => 'logs/command.log',

]);

/* ... */

Disabling a command

<?php

/* ... */

$jobby->add('DisabledExample', [
    
    'command' => 'ls',
    'schedule' => '0 * * * *',
    
    // You can turn off a job by setting 'enabled' to false
    'enabled' => false,

]);

/* ... */

Running closures

When running closures, beware that nothing outside of the closure is visible (see #93)!

<?php

/* ... */

$jobby->add('ClosureCommandExample', [
    
     // Use the 'closure' key
     // instead of 'command'
    'closure' => function() {
        echo "I'm a function!\n";
        return true;
    },
    
    'schedule' => '0 * * * *',

]);

/* ... */

Using a DateTime

<?php

/* ... */

$jobby->add('DateTimeExample', [
    
    'command' => 'ls',
    
    // Use a DateTime string in
    // the format Y-m-d H:i:s
    'schedule' => '2017-05-03 17:15:00',

]);

/* ... */

Using a Custom Scheduler

<?php

/* ... */

$jobby->add('Example', [
    
    'command' => 'ls',
    
    // Use any callable that returns
    // a boolean stating whether
    // to run the job or not
    'schedule' => function(DateTimeImmutable $now) {
        // Run on even minutes
        return $now->format('i') % 2 === 0;
    },

]);

/* ... */

Supported Options

Each job requires these:

KeyTypeDescription
schedulestringCrontab schedule format (man -s 5 crontab) or DateTime format (Y-m-d H:i:s) or callable (function(): Bool { /* ... */ })
commandstringThe shell command to run (exclusive-or with closure)
closureClosureThe anonymous PHP function to run (exclusive-or with command)

The options listed below can be applied to an individual job or globally through the Jobby constructor. Global options will be used as default values, and individual jobs can override them.

OptionTypeDefaultDescription
runAsstringnullRun as this user, if crontab user has sudo privileges
debugbooleanfalseSend jobby internal messages to 'debug.log'
FilteringOptions to determine whether the job should run or not
environmentstringnull or getenv('APPLICATION_ENV')Development environment for this job
runOnHoststringgethostname()Run jobs only on this hostname
maxRuntimeintegernullMaximum execution time for this job (in seconds)
enabledbooleantrueRun this job at scheduled times
haltDirstringnullA job will not run if this directory contains a file bearing the job's name
LoggingOptions for logging
outputstring/dev/nullRedirect stdout and stderr to this file
output_stdoutstringvalue from output optionRedirect stdout to this file
output_stderrstringvalue from output optionRedirect stderr to this file
dateFormatstringY-m-d H:i:sFormat for dates on jobby log messages
MailingOptions for emailing errors
recipientsstringnullComma-separated string of email addresses
mailerstringsendmailEmail method: sendmail or smtp or mail
smtpHoststringnullSMTP host, if mailer is smtp
smtpPortinteger25SMTP port, if mailer is smtp
smtpUsernamestringnullSMTP user, if mailer is smtp
smtpPasswordstringnullSMTP password, if mailer is smtp
smtpSecuritystringnullSMTP security option: ssl or tls, if mailer is smtp
smtpSenderstringjobby@<hostname>The sender and from addresses used in SMTP notices
smtpSenderNamestringJobbyThe name used in the from field for SMTP messages

Symfony integration

Symfony bundle for Jobby - imper86/jobby-cron-bundle

Credits

Developed before, but since inspired by whenever.

Support this project