| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- #!/usr/bin/env php
- <?php
- error_reporting(E_ALL);
- ini_set('display_errors', 'on');
- ini_set('limit_memory','512M');
- date_default_timezone_set('Asia/Shanghai');
- if(empty($argv[1]))
- {
- echo "Usage: workermand {start|stop|restart|reload|kill|status}".PHP_EOL;
- exit;
- }
- $cmd = $argv[1];
- define('WORKERMAN_ROOT_DIR', realpath(__DIR__."/../")."/");
- chdir(WORKERMAN_ROOT_DIR);
- if(0 === strpos('win', strtolower(PHP_OS)))
- {
- exit("Workerman can not run on Windows operating system\n");
- }
- if (!version_compare(PHP_VERSION, '5.3.0', '>='))
- {
- exit("Workerman PHP >= 5.3.0 required \n");
- }
- require_once WORKERMAN_ROOT_DIR . 'man/Core/Master.php';
- // ==pid-file==
- require_once WORKERMAN_ROOT_DIR . 'man/Core/Lib/Config.php';
- Man\Core\Lib\Config::instance();
- if(!($pid_file = Man\Core\Lib\Config::get('workerman.pid_file')))
- {
- $pid_file = '/var/run/workerman.pid';
- }
- define('WORKERMAN_PID_FILE', $pid_file);
- // ==log-dir==
- if(!($log_dir = Man\Core\Lib\Config::get('workerman.log_dir')))
- {
- $log_dir = WORKERMAN_ROOT_DIR . 'logs/';
- }
- define('WORKERMAN_LOG_DIR', $log_dir . '/');
- // ==ipc-key==
- if(!($ipc_key = Man\Core\Lib\Config::get('workerman.ipc_key')))
- {
- $ipc_key = fileinode(WORKERMAN_ROOT_DIR);
- }
- define('IPC_KEY', $ipc_key);
- // ==shm-size==
- if(!($shm_size = Man\Core\Lib\Config::get('workerman.shm_size')))
- {
- $shm_size = 393216;
- }
- define('DEFAULT_SHM_SIZE', $shm_size);
- //检查pid对应的进程是否存在,不存在删除PID文件
- if($cmd != 'status' && is_file(WORKERMAN_PID_FILE))
- {
- //检查权限
- if(!posix_access(WORKERMAN_PID_FILE, POSIX_W_OK))
- {
- if($stat = stat(WORKERMAN_PID_FILE))
- {
- if(($start_pwuid = posix_getpwuid($stat['uid'])) && ($current_pwuid = posix_getpwuid(posix_getuid())))
- {
- exit("\n\033[31;40mWorkerman is started by user {$start_pwuid['name']}, {$current_pwuid['name']} can not $cmd Workerman, Permission denied\033[0m\n\n\033[31;40mWorkerman $cmd failed\033[0m\n\n");
- }
- }
- exit("\033[31;40mCan not $cmd Workerman, Permission denied\033[0m\n");
- }
- //检查pid进程是否存在
- if($pid = @file_get_contents(WORKERMAN_PID_FILE))
- {
- if(false === posix_kill($pid, 0))
- {
- if(!unlink(WORKERMAN_PID_FILE))
- {
- exit("\033[31;40mCan not $cmd Workerman\033[0m\n\n");
- }
- }
- }
- }
- switch($cmd)
- {
- case 'start':
- $worker_user = isset($argv[2]) ? $argv[2] : '';
- Man\Core\Master::run($worker_user);
- break;
- case 'stop':
- $pid = @file_get_contents(WORKERMAN_PID_FILE);
- if(empty($pid))
- {
- exit("\033[33;40mWorkerman not running?\033[0m\n");
- }
- stop_and_wait();
- break;
- case 'restart':
- stop_and_wait();
- $worker_user = isset($argv[2]) ? $argv[2] : '';
- Man\Core\Master::run();
- break;
- case 'reload':
- $pid = @file_get_contents(WORKERMAN_PID_FILE);
- if(empty($pid))
- {
- exit("\033[33;40mWorkerman not running?\033[0m\n");
- }
- posix_kill($pid, SIGHUP);
- echo "reload Workerman\n";
- break;
- case 'kill':
- force_kill();
- force_kill();
- break;
- case 'status':
- $address = Man\Core\Lib\Config::get('Monitor.listen');
- $sock = @stream_socket_client($address);
- if(!$sock)
- {
- exit("\n\033[31;40mcan not connect to $address \033[0m\n\n\033[31;40mWorkerman not running\033[0m\n\n");
- }
- fwrite($sock, 'status');
- $read_fds = array($sock);
- $write_fds = $except_fds = array();
- while($ret = stream_select($read_fds, $write_fds, $except_fds, 1))
- {
- if(!$ret)break;
- foreach($read_fds as $fd)
- {
- if($ret_str = fread($fd, 8192))
- {
- echo $ret_str;
- }
- else
- {
- exit;
- }
- }
- }
- break;
- default:
- echo "Usage: workermand {start|stop|restart|reload|kill|status}\n";
- exit;
-
- }
- function force_kill()
- {
- $ret = $match = array();
- exec("ps aux | grep -E '".Man\Core\Master::NAME.":|workermand' | grep -v grep", $ret);
- $this_pid = posix_getpid();
- $this_ppid = posix_getppid();
- foreach($ret as $line)
- {
- if(preg_match("/^[\S]+\s+(\d+)\s+/", $line, $match))
- {
- $tmp_pid = $match[1];
- if($this_pid != $tmp_pid && $this_ppid != $tmp_pid)
- {
- posix_kill($tmp_pid, SIGKILL);
- }
- }
- }
- }
- function stop_and_wait($wait_time = 6)
- {
- $pid = @file_get_contents(WORKERMAN_PID_FILE);
- if(empty($pid))
- {
- //exit("server not running?\n");
- }
- else
- {
- $start_time = time();
- posix_kill($pid, SIGINT);
- while(is_file(WORKERMAN_PID_FILE))
- {
- clearstatcache();
- usleep(1000);
- if(time()-$start_time >= $wait_time)
- {
- force_kill();
- force_kill();
- unlink(WORKERMAN_PID_FILE);
- usleep(500000);
- break;
- }
- }
- echo "Workerman stoped\n";
- }
- }
|