I have a php script that executes commands to my CentOS server with process names
screen -dmS $processName ./build.sh
I want to be able to check if that process name is already running in screen and if it is running, do not execute the script but instead output a result back to php, for example
$processName = "myProcess";
//Check to see if the process is running using screen -ls and return yes or no back to the php script
if(!($stream = ssh2_exec($con, "screen -ls"))){
"there is a screen on: 11111.myProcess (Detached)" //Returns 'yes' to PHP
"No Sockets Found" //Returns 'no' to PHP
if($processReturn == "yes"){
//Do not start the process
echo "a process is already running in that name";
} else {
//start the process with the name myProcess
if(!($stream = ssh2_exec($con, "screen -dmS ".$processName." ./build.sh")));
}
UPDATE: Solved the issue myself, for those who may need it.
if(!empty($prCheck))
{
$proc = $_GET['prCheck'];
$stream = ssh2_exec($con, "screen -ls | grep ".$proc);
stream_set_blocking($stream, true);
$stream_out = ssh2_fetch_stream($stream, SSH2_STREAM_STDIO);
$output = stream_get_contents($stream_out);
$sep = explode('.', $output);
$result = explode(" (",$sep[1]);
$fin = $result[0];
if($fin == $proc){
echo $fin;
}
}
this gets just the process name, nothing else (no id, no dots, no spaces and no (Detached) areas)