A crontab written in PHP based on pcntl and react/event-loop
php crontab base on pcntl and react/event-loop
When we have a handful of crontab tasks, crontab service is enough for us to manage them. If we have many crontab tasks, there will be some problems like:
First composer require jenner/crontab
.
There are two ways to use php_crontab to manage your crontab tasks.
You can just write a php script and add it to the crontab config file
with the command crontab -e
. The php script should run every minute. For example tests/simple.php
Or you can write a php daemon script which will run as a service and will not exit until someone kill it.
It will check the tasks every minute. For example tests/daemon.php
composer require jenner/crontab
HTTP METHOD: GET
add
add new task to crontab serverget_by_name
get task by nameremove_by_name
remove task by nameclear
clear all taskget
get all tasksstart
start crontab loopstop
stop crontab loopExamples:
http://host:port/add?name=name&cmd=cmd&time=time&out=out&user=user&group=group&comment=comment
http://host:port/get_by_name?name=name
http://host:port/remove_by_name?name=name
http://host:port/clear
http://host:port/get
http://host:port/start
http://host:port/stop
run based on crontab service
* * * * * php demo.php
<?php
$missions = [
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'err' => '/tmp/php_crontab.log',
'time' => '* * * * *',
'user' => 'www',
'group' => 'www'
],
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'err' => '/tmp/php_crontab.log',
'time' => '* * * * *',
'user' => 'www',
'group' => 'www'
],
];
$tasks = array();
foreach($missions as $mission){
$tasks[] = new \Jenner\Crontab\Mission($mission['name'], $mission['cmd'], $mission['time'], $mission['out']);
}
$crontab_server = new \Jenner\Crontab\Crontab(null, $tasks);
$crontab_server->start(time());
run as a daemon
it will check the task configs every minute.
$missions = [
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'err' => '/tmp/php_crontab.log',
'time' => '* * * * *',
'user' => 'www',
'group' => 'www'
],
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'err' => '/tmp/php_crontab.log',
'time' => '* * * * *',
'user' => 'www',
'group' => 'www'
],
];
$daemon = new \Jenner\Crontab\Daemon($missions);
$daemon->start();
Or use the task loader
function task_loader() {
$missions = [
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'time' => '* * * * *',
'user' => 'www',
'group' => 'www'
],
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'time' => '* * * * *',
'user' => 'www',
'group' => 'www'
],
];
return $missions;
}
$daemon = new \Jenner\Crontab\Daemon();
$daemon->registerTaskLoader("task_loader");
$daemon->start();
run as a daemon and start the http server
$missions = [
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'err' => '/tmp/php_crontab.log',
'time' => '* * * * *',
'user' => 'www',
'group' => 'www'
],
[
'name' => 'ls',
'cmd' => "ls -al",
'out' => '/tmp/php_crontab.log',
'err' => '/tmp/php_crontab.log',
'time' => '* * * * *',
'user' => 'www',
'group' => 'www'
],
];
$http_daemon = new \Jenner\Crontab\HttpDaemon($missions, "php_crontab.log");
$http_daemon->start($port = 6364);
Then you can manage the crontab task by curl like:
curl http://127.0.0.1:6364/get_by_name?name=ls
curl http://127.0.0.1:6364/remove_by_name?name=hostname
curl http://127.0.0.1:6364/get
run the script
[root@jenner php_crontab]# ./bin/php_crontab
php_crontab help:
-c --config crontab tasks config file
-p --port http server port
-f --pid-file daemon pid file
-l --log crontab log file
[root@jenner php_crontab]#nohup ./bin/php_crontab -c xxoo.php -p 8080 -f /var/php_crontab.pid -l /var/logs/php_crontab.log >/dev/null &