I'm trying to display if certain processes are running in PHP, getting the process name from a txt file
<table style="width:959px;" border="3" cellspacing="1" cellpadding="1">
<tr>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Service Name</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Source Channel ID</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Webhook</font></b>
</td>
<td style="background-color:#A4A4A4;">
<b><font face="Arial, Helvetica, sans-serif">Status</font></b>
</td>
</tr>
<?php
$procfiles = file("proc.txt");
foreach ($procfiles as $profile){
$pro = explode("^", $profile);
exec("ps -aux | grep $pro[0] 2>&1", $output);
if (count($output) > 2) {
?>
<tr>
<td style="background-color:#E0A701;" ><?php echo $pro[0]; ?></td>
<td style="background-color:#E0A701;" ><?php echo $pro[1]; ?></td>
<td style="background-color:#E0A701;" > <div style="width: 500px; height: 40px; overflow: auto"><li><?php echo $pro[2]; ?></li></div></td>
<td style="background-color:#03D12F;">Running</td>
</tr>
<?php
}
else{
?>
<tr>
<td style="background-color:#E0A701;" ><?php echo $pro[0]; ?></td>
<td style="background-color:#E0A701;" ><?php echo $pro[1]; ?></td>
<td style="background-color:#E0A701;" > <div style="width: 500px; height: 40px; overflow: auto"><li><?php echo $pro[2]; ?></li></div></td>
<td style="background-color:#EF3636;">Not Running</td>
</tr>
<?php
}
}
?>
The problem is none of these processes are running. It checks if bot1.js is running or not fine, If I start bot1.js it will say its Running and if I stop it Not Running. But bot2.js and bot3.js, It always says it is running even when It's not. Im guessing I have to make it run the exec each time? How would I go about doing this? Cheers
Added print_r($output);
Results:
Array (
[0] => www-data 3670 0.0 0.0 4592 936 ? S 07:38 0:00 sh -c ps -aux | grep bot1.js 2>&1
[1] => www-data 3672 0.0 0.0 11396 1088 ? S 07:38 0:00 grep bot1.js
)
Array (
[0] => www-data 3670 0.0 0.0 4592 936 ? S 07:38 0:00 sh -c ps -aux | grep bot1.js 2>&1
[1] => www-data 3672 0.0 0.0 11396 1088 ? S 07:38 0:00 grep bot1.js
[2] => www-data 3673 0.0 0.0 4592 744 ? S 07:38 0:00 sh -c ps -aux | grep bot2.js 2>&1
[3] => www-data 3675 0.0 0.0 11396 972 ? S 07:38 0:00 grep bot2.js
)
Array (
[0] => www-data 3670 0.0 0.0 4592 936 ? S 07:38 0:00 sh -c ps -aux | grep bot1.js 2>&1
[1] => www-data 3672 0.0 0.0 11396 1088 ? S 07:38 0:00 grep bot1.js
[2] => www-data 3673 0.0 0.0 4592 744 ? S 07:38 0:00 sh -c ps -aux | grep bot2.js 2>&1
[3] => www-data 3675 0.0 0.0 11396 972 ? S 07:38 0:00 grep bot2.js
[4] => www-data 3676 0.0 0.0 4592 808 ? S 07:38 0:00 sh -c ps -aux | grep bot3.js 2>&1
[5] => www-data 3678 0.0 0.0 11396 1068 ? S 07:38 0:00 grep bot3.js
)
New Edit and info. Been trying to add what is suggested, but with no luck. Just testing with patterns etc. If I do this I get results shown.
$pattern = '/(\\/usr\\/bin\\/node\\s\\/home\\/james\\/bot-accounts\\/Testing\\/botname\\/bot1.js)/i';
$subject = '/usr/bin/node /home/james/bot-accounts/Testing/botname/bot1.js ';
$result = preg_match( $pattern, $subject , $matches );
echo $result;
print_r($matches);
But if I change the $subject to $output, I get no results. Its 'NULL'
exec("ps aux | grep $pro[0] | awk {'print $11, $12'}", $output);
$pattern = '/(\\/usr\\/bin\\/node\\s\\/home\\/james\\/bot-accounts\\/Testing\\/botname\\/bot1.js)/i';
$result = preg_match( $pattern, $output , $matches );
echo $result;
print_r($matches);

