PHP verification process id

I thought about something for a while and decided to ask about it.

We have a getmypid () function that will return the current script process id. Is there any function like

checkifpidexists () in php? I mean a built-in, not some kind of batch script solution.

And is there a way to change pid scripts?

Some explanations:

I want to check if pid exists, if the script exists, so it does not start again, faux cron job if you do this.

I want to change the pid, so I can set the pid script to something really high, like 60000 and hard code, so this script can only work on this pid, so only one instance of this will work

EDIT ----

To help anyone else with this proplem, I created this class:

class instance { private $lock_file = ''; private $is_running = false; public function __construct($id = __FILE__) { $id = md5($id); $this->lock_file = sys_get_temp_dir() . $id; if (file_exists($this->lock_file)) { $this->is_running = true; } else { $file = fopen($this->lock_file, 'w'); fclose($file); } } public function __destruct() { if (file_exists($this->lock_file) && !$this->is_running) { unlink($this->lock_file); } } public function is_running() { return $this->is_running; } } 

and you use it like this:

 $instance = new instance('abcd'); // the argument is optional as it defaults to __FILE__ if ($instance->is_running()) { echo 'file already running'; } else { echo 'file not running'; } 
+7
php process
source share
6 answers

On linux you can see / proc.

 return file_exists( "/proc/$pid" ); 

On Windows, you can use shell_exec () tasklist.exe and find the corresponding process id:

 $processes = explode( "\n", shell_exec( "tasklist.exe" )); foreach( $processes as $process ) { if( strpos( "Image Name", $process ) === 0 || strpos( "===", $process ) === 0 ) continue; $matches = false; preg_match( "/(.*?)\s+(\d+).*$/", $process, $matches ); $pid = $matches[ 2 ]; } 

I believe you want to save the PID file. In the daemons that I wrote, they check the configuration file, look for an instance of the pid file, pull the pid from the pid file, check if / proc / $ pid exists, and if not, delete the pid file.

  if( file_exists("/tmp/daemon.pid")) { $pid = file_get_contents( "/tmp/daemon.pid" ); if( file_exists( "/proc/$pid" )) { error_log( "found a running instance, exiting."); exit(1); } else { error_log( "previous process exited without cleaning pidfile, removing" ); unlink( "/tmp/daemon.pid" ); } } $h = fopen("/tmp/daemon.pid", 'w'); if( $h ) fwrite( $h, getmypid() ); fclose( $h ); 

Process identifiers are provided by the OS and it is not possible to reserve a process identifier. You must write your daemon to respect the pid file.

+17
source share

The best way to achieve this is to use a pid or lock file. Just check for the presence of the pid file, create it as needed and fill it with the current pid.

 <? class pidfile { private $_file; private $_running; public function __construct($dir, $name) { $this->_file = "$dir/$name.pid"; if (file_exists($this->_file)) { $pid = trim(file_get_contents($this->_file)); if (posix_kill($pid, 0)) { $this->_running = true; } } if (! $this->_running) { $pid = getmypid(); file_put_contents($this->_file, $pid); } } public function __destruct() { if ((! $this->_running) && file_exists($this->_file)) { unlink($this->_file); } } public function is_already_running() { return $this->_running; } } ?> 

And use it as follows:

 <? $pidfile = new pidfile('/tmp', 'myscript'); if($pidfile->is_already_running()) { echo "Already running.\n"; exit; } else { echo "Started...\n"; } ?> 

There are not many errors here, but a quick start shows that this works on my system.

+6
source share

No, you cannot change any pid processes. It is assigned by the kernel and is part of the kernel data structures.

+1
source share

To check for PID on a Windows machine, I use:

 function pidExists($pid) { exec('TASKLIST /NH /FO "CSV" /FI "PID eq '.$pid.'"', $outputA ); $outputB = explode( '","', $outputA[0] ); return isset($outputB[1])?true:false; } 

Note that $ outputB [0] contains messages that pid cannot be found if pid really does not exist! Therefore, to verify the use of the second argument.

+1
source share

As others have said, you cannot change the process identifier - it is assigned and completely controlled by the OS kernel. Also, you did not say that it is a command line server or a web server: if it is the last, you can not even get the pid of your script.

The man page for getmypid () contains some examples of optimistic blocking. I use the word optimisitc as PHP will never come close to a similar asp.net web application where you have a true streaming environment with common / static classes and thus Singleton for use / abuse. Basically you have the opportunity:

  • Touch the β€œlock file” in the file system somewhere. Then your script checks to see if this file exists: if it does, complete it, otherwise tap this file and continue processing
  • Setting up a database-based flag to say that the script is running. As stated above, but use the db / field table to mark the script as running.

Both of them rely on the script terminating correctly (as a last step, remove the / db lock flag). If the script crashes for some reason (or the machine itself), you can leave it manually to remove the flag. There is no simple solution for this, but one way to study would be to look at the date-stamping of the lock, with the conditional "if older than X, the last run should have been broken."

0
source share

Do not forget that you can access shell commands through return keys (`), which will give you access to standard * nix tools for working with pids.

source: http://www.php.net/manual/en/language.operators.execution.php

0
source share

All Articles