Skip to content

Commit acf68ab

Browse files
committed
update examples
1 parent 81d1852 commit acf68ab

4 files changed

Lines changed: 60 additions & 66 deletions

File tree

README.md

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -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

97105
it 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
149150
curl http://127.0.0.1:6364/get_by_name?name=hello
150151
curl 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**

example/daemon.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,19 @@
1212

1313
error_reporting(E_ALL);
1414

15-
$hello_command = "echo \"hello \";";
16-
$world_command = "sleep(1); echo \"world\";";
1715

1816
$missions = [
1917
[
20-
'name' => 'hello',
21-
'cmd' => "php -r '{$hello_command}'",
22-
'out' => '/tmp/www_php_crontab.log',
18+
'name' => 'ls',
19+
'cmd' => "ls -al",
20+
'out' => '/tmp/php_crontab.log',
2321
'time' => '* * * * *',
2422
'user' => 'www',
2523
'group' => 'www'
2624
],
2725
'mission_ls' => [
28-
'name' => 'world',
29-
'cmd' => "php -r '{$world_command}'",
26+
'name' => 'hostname',
27+
'cmd' => "hostname",
3028
'out' => '/tmp/php_crontab.log',
3129
'time' => '* * * * *',
3230
],

example/http_daemon.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,17 @@
1212

1313
error_reporting(E_ALL);
1414

15-
$hello_command = "echo \"hello \";";
16-
$world_command = "sleep(1); echo \"world\";";
1715

1816
$missions = [
1917
[
20-
'name' => 'hello',
21-
'cmd' => "php -r '{$hello_command}'",
18+
'name' => 'ls',
19+
'cmd' => "ls -al",
2220
'out' => '/tmp/php_crontab.log',
2321
'time' => '* * * * *',
2422
],
2523
[
26-
'name' => 'world',
27-
'cmd' => "php -r '{$world_command}'",
24+
'name' => 'hostname',
25+
'cmd' => "hostname",
2826
'out' => '/tmp/php_crontab.log',
2927
'time' => '* * * * *',
3028
],

example/simple.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,18 @@
1313

1414
error_reporting(E_ALL);
1515

16-
$hello_command = "sleep(1); echo 123;";
17-
$world_command = "sleep(1); echo 456 . PHP_EOL;";
18-
1916
$missions = [
2017
[
21-
'name' => 'hello',
22-
'cmd' => "php -r '{$hello_command}'",
18+
'name' => 'ls',
19+
'cmd' => "ls -al",
2320
'out' => '/tmp/php_crontab.log',
2421
'time' => '* * * * *',
2522
'user' => 'www',
2623
'group' => 'www'
2724
],
2825
'mission_ls' => [
29-
'name' => 'world',
30-
'cmd' => "php -r '{$world_command}'",
26+
'name' => 'hostname',
27+
'cmd' => "hostname",
3128
'out' => '/tmp/php_crontab.log',
3229
'time' => '* * * * *',
3330
],

0 commit comments

Comments
 (0)