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.
memnoch_proxy
source share