How to check php process in linux

Home / Check if a PHP script is already running

If you have long running batch processes with PHP that are run by cron and you want to ensure there’s only ever one running copy of the script, you can use the functions getmypid[] and posix_kill[] to check to see if you already have a copy of the process running. This post has a PHP class for checking if the script is already running.

Each process running on a Linux/Unix computer has a pid, or process identifier. In PHP this can be retrieved using getmypid[] which will return an integer number. This pid number can be saved to a file and each time the script is run a check made to see if the file exists. If it is the posix_kill[] function can be used to see if a process is running with that pid number.

My PHP class for doing this is below. Please feel free to use this and modify to suit your individual requirements.

class pid {

    protected $filename;
    public $already_running = false;
   
    function __construct[$directory] {
       
        $this->filename = $directory . '/' . basename[$_SERVER['PHP_SELF']] . '.pid';
       
        if[is_writable[$this->filename] || is_writable[$directory]] {
           
            if[file_exists[$this->filename]] {
                $pid = [int]trim[file_get_contents[$this->filename]];
                if[posix_kill[$pid, 0]] {
                    $this->already_running = true;
                }
            }
           
        }
        else {
            die["Cannot write to pid file '$this->filename'. Program execution halted.n"];
        }
       
        if[!$this->already_running] {
            $pid = getmypid[];
            file_put_contents[$this->filename, $pid];
        }
       
    }

    public function __destruct[] {

        if[!$this->already_running && file_exists[$this->filename] && is_writeable[$this->filename]] {
            unlink[$this->filename];
        }
   
    }
   
}

and it can be used as follows:

$pid = new pid['/tmp'];
if[$pid->already_running] {
    echo "Already running.n";
    exit;
}
else {
    echo "Running...n";
}

To test it, you could add "sleep[30];" to the end of the test script above, run the script and then run the script again. The second time you run it the script will output "Already running" and exit.

The class tidies up the pid file at the end of the script run automatically in the desctructor, so all you need to do is create the object at the start and then check to see if it’s already running.

Update 29 Oct 2008: I forgot to mention when I wrote this yesterday that posix_kill[] will return true if there is already a process running with $pid which is being run by the same user your script is running as. Pid numbers do get recycled when the limit is reached, so it is potentially possible [although unlikely] that you might be checking another process that was run by you and not the same exact PHP script. If you run the script as root querying any pid that is actually running will return true.

You can check for the process using ps, but one drawback is that if you have multiple instances of this process running or if the script is hung up then this method can be less than conclusive.

I prefer to actually check if the server is listening on the port. Here are a couple of ways to do this. If your server is listening on port 2000 for example consider the following.

Using lsof

lsof -i :2000; echo $?;

lsof is checking for open file descriptors and should show whether or not a program is listening or actively communicating on this port. This will echo either a 0 if the server is accepting connections on port 2000 or a 1 if it is not.

Using nc

nc -z -w1 192.168.1.12 2000 &> /dev/null; echo $?;

This is my preferred method for checking on a socket server. Here nc is using the -z flag for zero I/O mode to quickly scan the port. You can use your IP address here and the correct port. If the server is accepting connections then life is good.

Again here return values will be either a 0 for good or a 1 for not good. We are discarding any output here because we are wanting just a quick boolean check. This method returns very fast if the network address is reachable. Run from the server itself you will not see hardly any latency as it is trying to talk to itself.

Automating

To run these tests via cron, create a bash script and execute one or both of these commands and run through a series of logical checks. If they fail restart your script and recheck. I have been using these methods for several years now and have had very good results of practical uptime.

How do I know what PHP is doing?

On Windows you can check what php.exe is doing with Microsoft's procmon.exe. It won't give you a full feedback on variables etc, but you can check for any filesystem-operations [which php does very often]. PHP has some internal functions stored as extra .exe's. You can check with procmon if PHP call's them...

How do I know if PHP script is running?

Each process running on a Linux/Unix computer has a pid, or process identifier. In PHP this can be retrieved using getmypid[] which will return an integer number. This pid number can be saved to a file and each time the script is run a check made to see if the file exists.

How do I run PHP in Linux?

You just follow the steps to run PHP program using command line..
Open terminal or command line window..
Goto the specified folder or directory where php files are present..
Then we can run php code using the following command: php file_name.php..

How do I know if PHP is running Ubuntu?

Checking PHP version installed on your Linux and Unix server.
Open the terminal prompt and then type the following commands..
Login to the remote server using the ssh command. ... .
To check PHP version, run: php --version OR php-cgi --version..
To print PHP 7 version, type: php7 --version OR php7-cgi --version..

Bài mới nhất

Chủ Đề