I am running a php script to find out how many python3 processes are running on my machine. I use the following code:$count = exec("pgrep -c python3")
This returns me 2 (if 2 processes are running, for example). Now I need to do mathematical operation on this output, say subtract this count from 5. When I do so and echo the output, I get nothing. I tried typecasing to int, but that won't help.
Can somebody please suggest what I am doing wrong here?
Try the following function:
/**
* Count the number of processes runing based on their name
* @param string $processus_name
* @return integer|boolean
*/
function count_processus( $processus_name = '' ){
$processe_name = escapeshellarg( $processus_name );
$exit_status = 0;
$output = '';
$cmd = sprintf( 'pgrep -c %s', $processus_name );
exec( $cmd, $output, $exit_status );
if( $exit_status != 0 ){
// Faild $output will containe the error message
return false;
}
return (int)$output[0];
}