PHP code example of yzh52521 / thinkphp-cron

1. Go to this page and download the library: Download yzh52521/thinkphp-cron library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yzh52521 / thinkphp-cron example snippets




namespace app\task;

use yzh52521\cron\Task;

class DemoTask extends Task
{

    public function configure()
    {
        $this->daily(); //设置任务的周期,每天执行一次,更多的方法可以查看源代码,都有注释
    }

    /**
     * 执行任务
     * @return mixed
     */
    protected function execute()
    {
        //...具体的任务执行
    }
}


return [
    return [
        'type'  =>    'file',       //支持 file,mysql驱动
        'table' =>    'think_cron', //驱动类型为mysql时存储任务所用的表
        'cache' => 60,             //为数据库驱动时 查询缓存时间,为减数据库查询压力
        'tasks'   => [             //为文件存储时定时任务列表格式
            'demo'  =>[
                'title'     =>  '测试',
                'task'      =>  \app\task\DemoTask::class,
                'data'      =>  [],
                'expression'       =>  '* * * * *'
            ]
        ]
    ];
];

/**
 * 添加到计划任务
 * @param string $title 任务名
 * @param string $task  类名
 * @param array $data   参数 数组格式
 * @param string $expression 任务执行周期,使用标准的Crontab语法,默认60秒
 * @return bool
 */
 add_cron($title, $task, $data = [], $expression=null);

$data = ['name' => 'thinkphp'];
add_cron('test', \app\task\DemoTask::class, $data, '*/5 * * * *');

[program:php]
command= /usr/bin/php think cron:schedule start; 被监控进程
directory=/home/wwwroot/shabi.in
process_name=%(program_name)s
numprocs=1 ;启动几个进程 别改 扩展限制了一个进程运行
autostart=true ;随着supervisord的启动而启动
autorestart=true ;自动启动
startsecs=1 ;程序重启时候停留在runing状态的秒数
startretries=10 ;启动失败时的最多重试次数
redirect_stderr=true ;重定向stderr到stdout
stdout_logfile=/root/supervisor.log ;stdout文件