0 ){ shell_exec("kill -9 $masterPid"); //清空pid @unlink(self::$pidFile); self::$status = Wrapper::STOP; self::displayInfo(); }else{ exit("服务未启动,请先启动服务\n"); } } /** * 获取已经存在的pid * @return [type] [description] */ private static function getPidFromFile(){ if(file_exists(self::$pidFile)){ return file_get_contents(self::$pidFile); } } /** * 重启服务 * @return [type] [description] */ private static function restart(){ self::stop(); sleep(2); self::start(); } /** * 获取服务的状态 * @return [type] [description] */ private static function status(){ $pid = self::getPidFromFile(); if($pid > 0){ self::$status = Wrapper::RUNNING; }else{ self::$status = Wrapper::STOP; } self::displayInfo(); } /** * 重定向输出文件 * @return [type] [description] */ protected static function resetStd(){ if(!self::$daemonize){ return; } 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."不存在;"); } } /** * 判断服务是否已经启动 * @return boolean [description] */ protected static function isStart(){ $pid = self::getPidFromFile(); if( $pid > 0 ){ exit("服务已经启动,请先结束服务 \n"); } } /** * 保存pid * @return [type] [description] */ protected static function saveMasterPid(){ if(!self::$daemonize){ return; } file_put_contents(self::$pidFile,posix_getpid()); } /** * 尝试以守护进程的方式运行 * @throws Exception */ protected static function daemonize() { if(!self::$daemonize) { return; } $pid = pcntl_fork(); if($pid > 0) { exit(0); }else{ self::setProcName("PHP Wrapper Process @ ".self::$file); } } /** * 设置进程名称 * @param [type] $title [description] */ 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); } } private static function displayInfo(){ 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"; echo str_pad("执行脚本:",15," "),self::$file,"\n"; if(self::$status == Wrapper::RUNNING && self::$daemonize){ echo str_pad("进程PID路径:",15," "),self::$pidFile,"\n"; } echo str_pad("运行状态:",15," "),"\033[42;37m[",self::$status," SUCCESS]\033[0m\n"; echo "\n"; } }