Php implements continuous ping of remote server scripts and phpping scripts

Source: Internet
Author: User

Php implements continuous ping of remote server scripts and phpping scripts

Use Cases: the company's foreign servers are slow to access and have not accessed their resources for a long time, resulting in slow resolution access.

Script Function: continuously ping this type of server to ensure that the transit route can quickly respond to requests during service use.

<?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();?>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.