tags:

views:

1199

answers:

5

This is something i have wondered for a while and decided to ask about it.

We have the function getmypid() which will return the current scripts process id. Is there some kind of function such as

checkifpidexists() in php? I mean a inbuilt one and not some batch script solution.

And is there a way to change a scripts pid?

Some clarification:

I want to check if a pid exists to see if the script is already running so it dont run again, faux cron job if you will.

The reason i wanted to change the pid is so i can set the script pid to something really high such as 60000 and hard code that value so this script can only run on that pid so only 1 instance of it would run

EDIT----

To help anyone else with this proplem, i have 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 so:

$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';
}
+4  A: 

In linux, you would look at /proc.

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

In Windows you could shell_exec() tasklist.exe, and that would find a matching 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 );
     $pid = $matches[ 2 ];
}

I believe what you want to do is maintain a PID file. In the daemons I've written, they check a config file, look for an instance of a pid file, get the pid out of the pid file, check to see 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" );
   if( $h ) fwrite( $h, getmypid() );
   fclose( $h );

Process IDs are granted by the OS and one cannot reserve a process id. You would write your daemon to respect the pid file.

memnoch_proxy
pcntl_fork() will not change the current processes pid! The pcntl_fork() function creates a child process that differs from the parent process only in its PID and PPID. Please see your system's fork(2) man page for specific details as to how fork works on your system.
ennuikiller
hmm, i was hoping for a more cross os way of checking if a pid existed :(
Ozzy
With my edit, I mentioned I don't think he wants pcntl_fork(), nor would he get a choice of pid.
memnoch_proxy
A: 

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

ennuikiller
A: 

As others have said, you cannot change the process id - it is assigned and entirely manged by the kernel of the OS. Additionally, you have not said if this is command-line or web-server based: if it's the latter you may not even be getting the pid of your script.

The manual page for getmypid() contains some examples of "optimistic" locking. I use the word optimisitc as PHP is never ever going to approach the likes of an asp.net web application where you have a true threaded environment with shared/static classes and thus Singleton's to use/abuse. Basically you have the option of:

  • Touching a "lock file" on the file-system somewhere. Your script then checks if that file exists: if it does, terminate, otherwise, touch that file and carry on processing
  • Setting a database based flag to say the script is running. As above, but use a db table/field to mark a script as running.

Both of these rely on the script terminating correctly (as the last step would be to remove the lock file/db flag). If a script crashes for any reason (or the machine itself), you can be left with a manual tidy-up process to remove the flag. There is no easy solution for this, but one avenue to explore would be to then look at date-stamping the lock, with an arbitary "if older than X, the last run must have crashed" approach.

iAn
+2  A: 

A better way to accomplish this would be to use a pid or a lock file. Simply check for the existence of the pid file, create it as necessary, and populate it with your running 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's not much error checking here, but a quick run shows this works on my system.

jheddings
This is the method that many *nix daemons use.
Jason
A: 

Don't forget also that you can access shell commands via backticks (`), which would give you access to the standard *nix tools for working with pids.

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

phasetwenty