note 114191 added to function.stat
| From: | webmaster at askapache dot com | Date: | Wed, 22 Jan 2014 17:16:15 +0000 |
| Subject: | note 114191 added to function.stat | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-198304@lists.php.net to get a copy of this message | ||
On GNU/Linux you can retrieve the number of currently running processes on the machine by doing a
stat for hard links on the '/proc' directory like so:
$ stat -c '%h' /proc
118
You can do the same thing in php by doing a stat on /proc and grabbing the [3] 'nlink' -
number of links in the returned array.
Here is the function I'm using, it does a clearstatcache() when called more than once.
<?php
/**
* Returns the number of running processes
*
* @link http://php.net/clearstatcache
* @link http://php.net/stat Description of stat syntax.
* @author http://www.askapache.com/php/get-number-running-proccesses.html
* @return int
*/
function get_process_count() {
static $ver, $runs = 0;
// check if php version supports clearstatcache params, but only check once
if ( is_null( $ver ) )
$ver = version_compare( PHP_VERSION, '5.3.0', '>=' );
// Only call clearstatcache() if function called more than once */
if ( $runs++ > 0 ) { // checks if $runs > 0, then increments $runs by one.
// if php version is >= 5.3.0
if ( $ver ) {
clearstatcache( true, '/proc' );
} else {
// if php version is < 5.3.0
clearstatcache();
}
}
$stat = stat( '/proc' );
// if stat succeeds and nlink value is present return it, otherwise return 0
return ( ( false !== $stat && isset( $stat[3] ) ) ? $stat[3] : 0 );
}
?>
Example #1 get_process_count() example
<?php
$num_procs = get_process_count();
var_dump( $num_procs );
?>
The above example will output:
int(118)
Which is the number of processes that were running.
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.stat.php
Edit -- https://master.php.net/note/edit/114191
Del: integrated -- https://master.php.net/note/delete/114191/integrated
Del: useless -- https://master.php.net/note/delete/114191/useless
Del: bad code -- https://master.php.net/note/delete/114191/bad+code
Del: spam -- https://master.php.net/note/delete/114191/spam
Del: non-english -- https://master.php.net/note/delete/114191/non-english
Del: in docs -- https://master.php.net/note/delete/114191/in+docs
Del: other reasons-- https://master.php.net/note/delete/114191
Reject -- https://master.php.net/note/reject/114191
Search -- https://master.php.net/manage/user-notes.php