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';
}