@@ -68,74 +68,75 @@ http://host:port/stop
6868```
6969``` php
7070<?php
71- $crontab_config = [
72- 'test_1' => [
73- 'name' => 'test_1', //task name
74- 'cmd' => 'php -v', // cli command
75- 'output ' => '/tmp/test .log', // output file
76- 'time' => '* * * * *', //time config
77- 'user_name ' => 'www', //user
78- 'group_name ' => 'group_name', // group
71+ $missions = [
72+ [
73+ 'name' => 'ls',
74+ 'cmd' => "ls -al",
75+ 'out ' => '/tmp/php_crontab .log',
76+ 'time' => '* * * * *',
77+ 'user ' => 'www',
78+ 'group ' => 'www'
7979 ],
80- 'single_test' => [
81- 'name' => 'php -i',
82- 'cmd' => 'php -i',
83- 'output' => '/tmp/single_script.log',
84- 'time' => [
85- '* * * * *',
86- '* * * * *',
87- ],
80+ 'mission_ls' => [
81+ 'name' => 'hostname',
82+ 'cmd' => "hostname",
83+ 'out' => '/tmp/php_crontab.log',
84+ 'time' => '* * * * *',
8885 ],
8986];
90-
91- $time = time();
92- $crontab_server = new \Jenner\Zebra\Crontab\Crontab($crontab_config);
93- $crontab_server->start($time);
87+ $logger = new \Monolog\Logger("php_crontab");
88+ $logger->pushHandler(new \Monolog\Handler\StreamHandler("/tmp/php_crontab.log"));
89+
90+ $tasks = array();
91+ foreach($missions as $mission){
92+ $tasks[] = new \Jenner\Crontab\Mission(
93+ $mission['name'],
94+ $mission['cmd'],
95+ $mission['time'],
96+ $mission['out']
97+ );
98+ }
99+
100+ $crontab_server = new \Jenner\Crontab\Crontab($logger, $tasks);
101+ $crontab_server->start(time());
94102```
95103** run as a daemon**
96104
97105it will check the task configs every minute.
98106``` php
99- $crontab_config = [
100- 'test_1' => [
101- 'name' => 'test_1 ',
102- 'cmd' => 'php -r "echo "11111" . PHP_EOL;sleep(60);"' ,
103- 'output ' => '/www/test .log',
107+ $missions = [
108+ [
109+ 'name' => 'ls ',
110+ 'cmd' => "ls -al" ,
111+ 'out ' => '/tmp/php_crontab .log',
104112 'time' => '* * * * *',
105- 'user_name ' => 'www',
106- 'group_name ' => 'www'
113+ 'user ' => 'www',
114+ 'group ' => 'www'
107115 ],
108- 'single_test' => [
109- 'name' => 'php -i',
110- 'cmd' => 'php -i',
111- 'output' => '/tmp/single_script.log',
112- 'time' => [
113- '* * * * *',
114- '* * * * *',
115- ],
116+ 'mission_ls' => [
117+ 'name' => 'hostname',
118+ 'cmd' => "hostname",
119+ 'out' => '/tmp/php_crontab.log',
120+ 'time' => '* * * * *',
116121 ],
117122];
118123
119- $daemon = new \Jenner\Zebra\ Crontab\Daemon($crontab_config, "logfile.log" );
124+ $daemon = new \Jenner\Crontab\Daemon($missions );
120125$daemon->start();
126+ ```
121127
122128** run as aa daemon and start the http server**
123129``` php
124- error_reporting(E_ALL);
125-
126- $hello_command = "echo \"hello \";";
127- $world_command = "sleep(1); echo \"world\";";
128-
129130$missions = [
130131 [
131- 'name' => 'hello ',
132- 'cmd' => "php -r '{$hello_command}' ",
132+ 'name' => 'ls ',
133+ 'cmd' => "ls -al ",
133134 'out' => '/tmp/php_crontab.log',
134135 'time' => '* * * * *',
135136 ],
136137 [
137- 'name' => 'world ',
138- 'cmd' => "php -r '{$world_command}' ",
138+ 'name' => 'hostname ',
139+ 'cmd' => "hostname ",
139140 'out' => '/tmp/php_crontab.log',
140141 'time' => '* * * * *',
141142 ],
@@ -148,7 +149,7 @@ Then you can manage the crontab task by curl like:
148149``` shell
149150curl http://127.0.0.1:6364/get_by_name? name=hello
150151curl http://127.0.0.1:6364/remove_by_name? name=hello
151- curl http://127.0.0.1:6364/missions
152+ curl http://127.0.0.1:6364/get
152153```
153154
154155** run the script**
0 commit comments