Hello, please sign in or register
You are here: Home

PHP pid detection

To prevent duplicate php processes from running. Save the following script to a file and include it from your script that is being called.

You will need to define the directory where temporary files used to store the PID's

<?php
/****************************************************************
 * Get the current PID and other instances of the script running?
 ****************************************************************/
$pid_file = DATA . 'pid/' . basename( $_SERVER['PHP_SELF'] ) . '.pid';

/**
 * Get the process id from the "script_file_name.pid" file. If it exists
 */
exec( "less  " . $pid_file , $a );
$pid = current( $a );
unset( $a );

/**
 * If the process ID is in the systems processlist. Exit this script, allowing the other to continue
 */
if ( !empty( $pid ) )
{
	exec( "ps -p $pid -o comm=", $a );

	if ( $a )
	{
		exec( "ps -p $pid -f", $a );
		r(  "An instance of this script '" . basename( $_SERVER['PHP_SELF'] ) . "'is already running", $a, ( ( time() - filemtime( $pid_file ) ) > 3600*12 ? EXITING : COMPLETE ) );
	}
	unset( $a );
}

/**
 * Get the UID of the current process.  * Overwrite "script_file_name.pid" with the new value.
 */
exec( "echo " . getmypid() . " > " . $pid_file );
?>

Comments

wq123
This hilfiger outlet time swarovski jewelry something really timberland outlet strange, ...
Created 30/06/16
ninestab123
ninest123 One canada goose pas cher gucci outlet thing
Created 21/07/16
ZZZZ
michael kors handbags nike trainers
Created 05/10/16
Title*
Comment

Prove you are not a robot

To prove you are not a robot, please type in the six character code you see in the picture below
Security confirmation codeI can't see this!
Contact
Name*
Email never shown*
Home Page

Author

Andrew Dodson
Since:Feb 2007

Comment | flag

Categories

Bookmark and Share