Checking if process still running?

后端 未结 9 577
野的像风
野的像风 2020-11-30 03:55

As a way to build a poor-man\'s watchdog and make sure an application is restarted in case it crashes (until I figure out why), I need to write a PHP CLI script that will be

9条回答
  •  情书的邮戳
    2020-11-30 04:34

    I'd use pgrep to do this (caution, untested code):

    
    exec("pgrep lighttpd", $pids);
    if(empty($pids)) {
    
        // lighttpd is not running!
    }
    
    

    I have a bash script that does something similar (but with SSH tunnels):

    
    #!/bin/sh
    
    MYSQL_TUNNEL="ssh -f -N -L 33060:127.0.0.1:3306 tunnel@db"
    RSYNC_TUNNEL="ssh -f -N -L 8730:127.0.0.1:873 tunnel@db"
    
    # MYSQL
    if [ -z `pgrep -f -x "$MYSQL_TUNNEL"` ] 
    then
        echo Creating tunnel for MySQL.
        $MYSQL_TUNNEL
    fi
    
    # RSYNC
    if [ -z `pgrep -f -x "$RSYNC_TUNNEL"` ]
    then
        echo Creating tunnel for rsync.
        $RSYNC_TUNNEL
    fi
    
    
    

    You could alter this script with the commands that you want to monitor.

提交回复
热议问题