Skip to content

Commit e566de3

Browse files
committed
init
0 parents  commit e566de3

File tree

7 files changed

+497
-0
lines changed

7 files changed

+497
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
composer.phar
2+
composer.lock
3+
/vendor/
4+
*.sw[pon]
5+
tags

LICENSE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Copyright 2017 Chopin Ngo<consatan@gmail.com>
2+
3+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4+
5+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6+
7+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8+
9+
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10+
11+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#### PDaemon
2+
Running PHP script in daemon mode at CLI. **Not available on Windows platforms**.
3+
4+
#### Install
5+
6+
##### Requirements
7+
8+
- PHP >= 5.4
9+
- pcntl extension
10+
- posix extension
11+
- sysvsem extension
12+
13+
##### Use composer (recommend)
14+
15+
```shell
16+
composer require consatan/pdaemon
17+
```
18+
19+
##### Download from github
20+
21+
```shell
22+
git clone https://consatan.github.com/pdaemon.git
23+
```
24+
25+
26+
#### How to use
27+
28+
Simple daemon
29+
```php
30+
<?php
31+
32+
require './vendor/autoload.php';
33+
34+
class DemoDaemon extends \Consatan\PDaemon\PDaemon
35+
{
36+
protected function exec()
37+
{
38+
// Your code here.
39+
echo 'Running in children, PID: ' . posix_getpid() . PHP_EOL;
40+
// You can access your custom variable in $this->options
41+
// echo $this->options['your_variable'] . PHP_EOL;
42+
sleep(3);
43+
}
44+
}
45+
46+
// Fork 3 children processes, pid file in /var/run/pdaemon.pid
47+
$daemon = new DemoDaemon(['pool' => 3, 'your_variable' => 'hello world']);
48+
$daemon->run();
49+
```
50+
51+
You can stop daemon process list this
52+
```php
53+
<?php
54+
55+
require './vendor/autoload.php';
56+
57+
// replace to your pid path
58+
$pid = file_get_contents('/var/run/pdaemon.pid');
59+
60+
if (false === $pid) {
61+
// pid file cannot read or not exists.
62+
exit(-1);
63+
}
64+
65+
$pid = (int)$pid;
66+
67+
if (0 >= $pid) {
68+
// invalid pid
69+
exit(-1);
70+
}
71+
72+
// This is a safe terminat function, it will stop process when all children processes stop
73+
posix_kill($pid, SIGTERM);
74+
75+
// You can forced stop the daemon like this: (non-recommended);
76+
// posix_kill($pid, SIGKILL);
77+
```
78+
79+
#### Todo
80+
81+
- [ ] Unit test
82+
- [ ] Handling reload signal
83+
- [ ] Handling status signal

VERSION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

composer.json

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "consatan/pdaemon",
3+
"description": "PHP Daemon, is not available on Windows platforms.",
4+
"keywords": ["daemon", "cli"],
5+
"type": "library",
6+
"license": "BSD 3-Clause",
7+
"authors": [
8+
{
9+
"name": "Chopin Ngo",
10+
"email": "consatan@gmail.com",
11+
"homepage": "https://chopin.im"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.4",
16+
"ext-pcntl": "*",
17+
"ext-posix": "*",
18+
"ext-sysvsem": "*"
19+
},
20+
"autoload": {
21+
"psr-4": {"Consatan\\PDaemon\\": "src/"}
22+
}
23+
}

0 commit comments

Comments
 (0)