Justin Jones
Justin Jones

Reputation: 15

Creating loop inside table. PHP

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
        }
    }
    ?>

proc.txt enter image description here

The outcome enter image description here

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);

Upvotes: 0

Views: 114

Answers (3)

Herbert Scheffknecht
Herbert Scheffknecht

Reputation: 631

As you see with var_dump, exec is always adding the output of each call to $output, but does not replace it. So add $output =[] before the exec command.

Upvotes: 0

zen
zen

Reputation: 992

As you can see, just counting results from ps -aux is not best way to check running.

Check result with preg_match with pattern like #\d*\s\d*\s\?\s(\w).*?<here name of file>#is. And if one of results is R (i.e. running), then pass check of run.

Upvotes: 1

Tejashwi Kalp Taru
Tejashwi Kalp Taru

Reputation: 3064

Every process will be listed in the output of ps aux; whether running, sleeping, zombie or stopped.

However, in your case, since you are checking for JS file, I guess you used something to run that JS, right?

You should note that the process will be "running" when the output of ps aux has its STAT as R

Ref:https://askubuntu.com/a/473891

If you can provide a clue about how that processes i.e. bot1.js and others started or how you ran those, it might help to pinpoint exact command

Upvotes: 1

Related Questions