php设置定时任务, php任务管理, php crontab jobs, PHP cron job manager

packages: https://packagist.org/packages/hellogerard/jobby

项目地址:https://github.com/jobbyphp/jobby

直接下载:jobby-master

PHP cron job manager

 

1. composer安装

composer require hellogerard/jobby

添加任务:

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

复制sample文件,到项目根目录 (即:上面的/path/to/project)

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

 

2. 运行一个实例

<?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() {
        // Run on even minutes
        return date('i') % 2 === 0;
    },

]);

/* ... */

php设置定时任务, php任务管理, php crontab jobs, PHP cron job manager
php设置定时任务, php任务管理, php crontab jobs, PHP cron job manager

支持的选项 Supported Options

Each job requires these:

Key Type Description
schedule string Crontab schedule format (man -s 5 crontab) or DateTime format (Y-m-d H:i:s) or callable (function(): Bool { /* ... */ })
command string The shell command to run (exclusive-or with closure)
closure Closure The 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.

Option Type Default Description
runAs string null Run as this user, if crontab user has sudo privileges
debug boolean false Send jobby internal messages to ‘debug.log’
Filtering Options to determine whether the job should run or not
environment string null or getenv('APPLICATION_ENV') Development environment for this job
runOnHost string gethostname() Run jobs only on this hostname
maxRuntime integer null Maximum execution time for this job (in seconds)
enabled boolean true Run this job at scheduled times
haltDir string null A job will not run if this directory contains a file bearing the job’s name
Logging Options for logging
output string /dev/null Redirect stdout and stderr to this file
dateFormat string Y-m-d H:i:s Format for dates on jobby log messages
Mailing Options for emailing errors
recipients string null Comma-separated string of email addresses
mailer string sendmail Email method: sendmail or smtp or mail
smtpHost string null SMTP host, if mailer is smtp
smtpPort integer 25 SMTP port, if mailer is smtp
smtpUsername string null SMTP user, if mailer is smtp
smtpPassword string null SMTP password, if mailer is smtp
smtpSecurity string null SMTP security option: ssl or tls, if mailer is smtp
smtpSender string jobby@<hostname> The sender and from addresses used in SMTP notices
smtpSenderName string Jobby The name used in the from field for SMTP messages

本文:php设置定时任务, php任务管理, php crontab jobs, PHP cron job manager

Loading

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.