Home  >  Article  >  Backend Development  >  php实现连续ping远程服务器脚本

php实现连续ping远程服务器脚本

WBOY
WBOYOriginal
2016-06-23 13:31:07864browse

使用场景:公司的国外服务器访问缓慢,较长时间未访问其资源,导致解析访问缓慢。

脚本作用:持续ping该类服务器,保证中转路由能在业务使用时能够快速响应请求。

<?phpdefine ('LOGTAG', 'ping');declare(ticks = 1);$pid_arr = array();pcntl_signal(SIGQUIT, 'signal_handler');pcntl_signal(SIGTERM, 'signal_handler');function run($servers_info) {    echo "---------- ping task begin ----------";    global $pid_arr;    $index=0;    $name_arr = array();    $ip_arr = array();    foreach( $servers_info as $val) {        $name_arr[$index] = $val['server_name'];        $ip_arr[$index] = $val['server_ip'];        $index++;    }    $worker_count = $index;    $index=0;    while($index < $worker_count)     {        $pid = pcntl_fork();        if($pid == -1) {            die('could not fork');        }         else         {            if($pid) {                $pid_arr[$index] = $pid;            } else {                while(true) {                    handle($name_arr[$index], $ip_arr[$index]);                    sleep(1);                }            }        }        $index++;    }    while (true) {        sleep(1);    }}function handle($name,$ip) {    echo "ping ".$name." ip:".$ip." start!";    exec("ping -c 1000 $ip",$list);    echo "ping ".$name." ip:".$ip." finish!";}function signal_handler($signal) {    global $pid_arr;    if ($signal == SIGQUIT || $signal == SIGTERM)     {        foreach ($pid_arr as $pid) {            posix_kill($pid,SIGTERM);        }        echo ”????? ping task finish ----------";        exit();    }}run();?>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn