[PHP 4, PHP 5, PHP 7, PHP 8] getmypid — Gets PHP's process ID getmypid[]: int|false This function has no parameters. Returns the current PHP process ID, or WarningDescription
Parameters
Return Values
false
on error. Notes
Process IDs are not unique, thus they are a weak entropy source. We recommend against relying on pids in security-dependent contexts.
See Also
- getmygid[] - Get PHP script owner's GID
- getmyuid[] - Gets PHP script owner's UID
- get_current_user[] - Gets the name of the owner of the current PHP script
- getmyinode[] - Gets the inode of the current script
- getlastmod[] - Gets time of last page modification
Radu Cristescu ¶
9 years ago
The lock-file mechanism in Kevin Trass's note is incorrect because it is subject to race conditions.
For locks you need an atomic way of verifying if a lock file exists and creating it if it doesn't exist. Between file_exists and file_put_contents, another process could be faster than us to write the lock.
The only filesystem operation that matches the above requirements that I know of is symlink[].
Thus, if you need a lock-file mechanism, here's the code. This won't work on a system without /proc [so there go Windows, BSD, OS X, and possibly others], but it can be adapted to work around that deficiency [say, by linking to your pid file like in my script, then operating through the symlink like in Kevin's solution].
#!/usr/bin/php
If you are sharing /tmp over the network [which is odd....] then you can, of course, mix in the PHP server's IP address.
Kevin Traas [ktraas- at -gmail dot com] ¶
12 years ago
Looking to create a lock-file mechanism for a cmd-line script?
Enjoy!
#!/usr/bin/php