From 13c725397d53f0d077e66183f716a14350715a1e Mon Sep 17 00:00:00 2001 From: joinwong Date: Thu, 22 Sep 2016 13:35:55 +0800 Subject: [PATCH 1/2] posix_setsid --- wrapper/helper.php | 206 ++++++++++++++++++++++++++++++++++++++++++++ wrapper/wrapper.php | 7 ++ 2 files changed, 213 insertions(+) create mode 100644 wrapper/helper.php diff --git a/wrapper/helper.php b/wrapper/helper.php new file mode 100644 index 0000000..aabba7d --- /dev/null +++ b/wrapper/helper.php @@ -0,0 +1,206 @@ +0){ + exit("服务已经启动了,请先停止服务\n"); + } + break; + case 'stop': + self::stop(); + break; + case 'reload': + posix_kill($masterPid,SIGUSR1); + exit; + default: + exit("please input {start|stop|reload} \n"); + } + } + + private static function stop(){ + @unlink(self::$pidFile); + exec("ps aux|grep Helper:|grep -v 'grep Helper:'|awk '{print $2}'|xargs kill -9"); + } + + private static function daemonize(){ + if(!self::$daemonize){ + return; + } + $pid = pcntl_fork(); + if($pid == -1 ){ + throw new \Exception("Fork Error\n"); + } + if($pid > 0){ + exit(0); + } + + if(posix_setsid() == -1){ + throw new \Exception("posix setsid error\n"); + } + + $pid = pcntl_fork(); + if($pid == -1){ + throw new \Exception("fork error\n"); + } + elseif($pid != 0){ + exit(0); + } + } + + private static function installSignal(){ + pcntl_signal(SIGINT,array('Helper','signalHandler'),false); + pcntl_signal(SIGUSR1,array('Helper','signalHandler'),false); + pcntl_signal(SIGUSR2,array('Helper','signalHandler'),false); + } + + private static function signalHandler($signal){ + switch($signal){ + //stop + case SIGINT: + self::stopAll(); + break; + //reload + case SIGUSR1: + self::reload(); + break; + //status + case SIGUSR2: + break; + } + } + + + /** + * reload + */ + private static function reload(){ + if(self::$_masterPid == posix_getpid()){ + //主进程 + foreach(self::$_pidMap as $pid){ + //posix_kill($pid,SIGUSR1); + } + } + } + + private static function forkWorkers(){ + for($i = 0;$i 0){ + self::$_pidMap[]=$pid; + }elseif($pid == 0){ + self::setProcessName("Helper:Process"); + self::resetStd(); + //子进程do something + self::doSth(); + exit(200); + } + } + + private static function doSth(){ + while(1){ + pcntl_signal_dispatch(); + //echo ""; + echo "i am doSth before @ ",date('Y-m-d H:i:s'),"\n"; + sleep(20); + echo "i am doSth after @ ",date('Y-m-d H:i:s'),"\n"; + } + } + + private static function saveMasterPid(){ + self::$_masterPid = posix_getpid(); + if(@file_put_contents(self::$pidFile,self::$_masterPid) === false){ + throw new \Exception('can not save pid to '.self::$pidFile."\n"); + } + } + + private static function info(){ + echo "--------------------------------------------\n"; + echo "\033[30;47m","This is Helper","\033[0m","\n"; + echo "Master Pid:",self::$_masterPid,"\n"; + echo "Child Pid:",implode(",",self::$_pidMap),"\n"; + echo "--------------------------------------------\n"; + } + + private static function resetStd(){ + if(!self::$daemonize){ + return; + } + + global $STDOUT,$STDERR; + + $handle = @fopen(self::$logFile,'a'); + if($handle){ + @fclose(STDOUT); + @fclose(STDERR); + unset($handle); + $STDOUT=fopen(self::$logFile,'a'); + $STDERR=fopen(self::$logFile,'a'); + }else{ + throw new \Exception("不能打开stdOutFile ". self::$logFile."\n"); + } + } + + private static function monitor(){ + while(1){ + pcntl_signal_dispatch(); + $status = 0; + $pid = pcntl_wait($status,WUNTRACED); + pcntl_signal_dispatch(); + // + //sleep(10); + } + } + +} \ No newline at end of file diff --git a/wrapper/wrapper.php b/wrapper/wrapper.php index 08d94b2..b593efc 100644 --- a/wrapper/wrapper.php +++ b/wrapper/wrapper.php @@ -232,6 +232,13 @@ protected static function daemonize() if($pid > 0) { exit(0); + } + + posix_setsid(); + + $pid = pcntl_fork(); + if($pid > 0){ + exit(0); }else{ self::setProcName("PHP Wrapper Process @ ".self::$file); } From d8c4e7e1961c5af12a0eb84b832aaf7ec6089a3c Mon Sep 17 00:00:00 2001 From: wangjun Date: Fri, 28 Jul 2017 22:31:38 +0800 Subject: [PATCH 2/2] format code --- README.md | 24 ++++++++++++------------ wrapper/wrapper.php | 45 +++++++++++++++++++-------------------------- 2 files changed, 31 insertions(+), 38 deletions(-) diff --git a/README.md b/README.md index 2edf0ea..0e349c2 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,18 @@ -# phpServerWrapper +## phpServerWrapper -### 之前部署php服务一直都是shell脚本 - -### 此框架可以直接利用posix进行进程部署 +### 此框架可以直接利用pcntl、posix进行进程部署 #### 只有简单的start、stop命令 -### 依赖 -#### posix -#### pcntl +#### 依赖 +* posix +* pcntl -### 启动文件配置 +#### 启动文件配置 -start.php +* start.php +``` //加载文件 require_once './wrapper/wrapper.php'; @@ -25,8 +24,9 @@ Wrapper::$masterPidPath = '/tmp/testShell.pid'; Wrapper::$file = "./shell/testShell.php"; Wrapper::run(); +``` - -### 启动start.php - +#### 启动start.php +``` php ./start.php start +``` \ No newline at end of file diff --git a/wrapper/wrapper.php b/wrapper/wrapper.php index b593efc..242af8e 100644 --- a/wrapper/wrapper.php +++ b/wrapper/wrapper.php @@ -31,9 +31,8 @@ public static function run(){ } private static function init(){ - if(empty(self::$logFile)) - { - self::$logFile = __DIR__ . '/../wrapper.log'; + if(empty(self::$logFile)){ + self::$logFile = __DIR__ . '/../wrapper.log'; } } @@ -116,7 +115,7 @@ private static function stop(){ $masterPid = self::getPidFromFile(); if( $masterPid >0 ){ - shell_exec("kill -9 $masterPid"); + shell_exec("kill -9 $masterPid"); //清空pid @unlink(self::$pidFile); @@ -176,17 +175,14 @@ protected static function resetStd(){ global $STDIN,$STDOUT; $handle = fopen(self::$logFile,"a"); - if($handle) - { - unset($handle); - @fclose(STDOUT); - @fclose(STDERR); - $STDOUT = fopen(self::$logFile,"a"); - $STDERR = fopen(self::$logFile,"a"); - } - else - { - exit("日志文件".self::$logFile."不存在;"); + if($handle){ + unset($handle); + @fclose(STDOUT); + @fclose(STDERR); + $STDOUT = fopen(self::$logFile,"a"); + $STDERR = fopen(self::$logFile,"a"); + }else{ + exit("日志文件".self::$logFile."不存在;"); } } @@ -223,15 +219,13 @@ protected static function saveMasterPid(){ */ protected static function daemonize() { - if(!self::$daemonize) - { - return; + if(!self::$daemonize){ + return; } $pid = pcntl_fork(); - if($pid > 0) - { - exit(0); + if($pid > 0){ + exit(0); } posix_setsid(); @@ -240,7 +234,7 @@ protected static function daemonize() if($pid > 0){ exit(0); }else{ - self::setProcName("PHP Wrapper Process @ ".self::$file); + self::setProcName("PHP Wrapper Process @ ".self::$file); } } @@ -252,15 +246,14 @@ private static function setProcName($title){ if(function_exists("cli_set_process_title")){ @cli_set_process_title($title); } - elseif(extension_loaded('proctitle') && function_exists('setproctitle')) - { - @setproctitle($title); + elseif(extension_loaded('proctitle') && function_exists('setproctitle')){ + @setproctitle($title); } } private static function displayInfo(){ - echo "============================\033[47;30m Wrapper \033[0m============================\n"; + echo "============================\033[47;30m Wrapper \033[0m============================\n"; echo str_pad("Wrapper版本:" ,15," "), Wrapper::VERSION,"\n"; echo str_pad("当前时间:",15," "),date('Y-m-d H:i:s'),"\n"; echo str_pad("PHP版本:",15," "),PHP_VERSION,"\n";