apolinux / daemon
此软件包的最新版本(v0.40)没有提供许可证信息。
类似于Unix系统守护进程的守护进程管理器
v0.40
2020-03-24 03:00 UTC
This package is auto-updated.
Last update: 2024-09-24 13:09:57 UTC
README
创建系统守护进程过程。守护进程程序是一种特殊系统程序,它在后台执行,所有输入数据都被避免,输出保存到日志文件或其他类似文件中。
此类允许控制守护进程程序的启动和停止以及与守护进程相关的其他命令。它可以查看状态以检查它是否正在运行。还允许在前台运行程序以查看代码中是否存在任何问题。
详细信息
要守护的程序可以是以下之一
- 调用一次的函数
- 在循环中重复调用的函数
- 使用系统fork调用一次的函数
- 使用pcntl_exec运行一次的命令行程序
- 使用pcntl_exec和fork运行一次的命令行程序
- 使用pcntl_exec和fork在循环中运行的命令行程序
说明
-
系统fork:它是一种从调用时开始复制运行进程的方法,因此可以有两个运行程序的副本。它用于pcntl_fork()函数。
-
pcntl_exec:当使用时,当前运行的程序被调用命令替换。
可以从匿名函数、现有函数或使用PHP脚本调用子进程。
示例
运行一次的守护任务
代码
<?php
use Apolinux\DaemonAdmin\DaemonAdmin;
use Apolinux\DaemonAdmin\TaskManager;
require __DIR__ .'/../vendor/autoload.php' ;
$procname = substr(basename(__FILE__), 0, -4);
$daemon = new DaemonAdmin([
'pid_file' => __DIR__ ."/var/$procname.pid" ,
'log_dir' => __DIR__ ."/var" ,
'name' => $procname ,
'task_mode' => TaskManager::MODE_ONCE_CALL ,
'task' => 'testTask'
]);
function testTask(){
while(1){
echo "start task\n" ;
$f = tmpfile();
for($i=1 ; $i<=40000; $i++){
fwrite($f, md5(base64_decode(random_bytes(256)))) ;
}
echo "file created\n" ;
fseek($f,0) ;
while(! feof($f)){
$null = fgetc($f);
unset($null);
}
fclose($f);
echo "file closed\n" ;
}
}
$daemon->run();
使用示例
>php examples/DaemonTestOnceCall.php
DaemonTestOnceCall Daemon
Runs a process in background and controlls it
Usage: DaemonTestOnceCall.php start|stop|restart|status|help|h
start : starts the daemon
stop : stops the daemon
restart: stop, then restart daemon
status : shows daemon info if it is running
fg : start in foreground, no daemon
help,h : shows this help
>php examples/DaemonTestOnceCall.php start
Starting Daemon
Daemon started
>php examples/DaemonTestOnceCall.php status
process is running with pid: 28572
>php examples/DaemonTestOnceCall.php restart
Stopping daemon
Daemon stopped
Starting Daemon
Daemon started
>php examples/DaemonTestOnceCall.php stop
Stopping daemon
Daemon stopped
>php examples/DaemonTestOnceCall.php status
process is not running
>php examples/DaemonTestOnceCall.php fg
[ 2020-03-23 21:57:00.5051 ] Child started with pid:29144
start task
file created
file closed
start task
file created
file closed
start task
file created
^C
示例列表
- DaemonTestException.php:在一个循环中运行测试任务,但抛出异常
- DaemonTestFail.php:在一个循环中运行测试任务,但引发自定义错误
- DaemonTestForkWaitFail.php:在分叉循环中运行测试任务,但引发自定义错误
- DaemonTestLoopCallFork.php:在分叉循环中运行测试任务
- DaemonTestLoopCall.php:在循环中运行测试任务
- DaemonTestOnceCall.php:运行一次测试任务并完成
- DaemonTestOnceCmd.php:运行一次测试命令并完成