0

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

4
  • Can you print_r($output)? Commented Jun 8, 2018 at 4:54
  • Added in main post Commented Jun 8, 2018 at 5:41
  • Try by using an ajax request by setting an interval of 5 seconds. Commented Jun 8, 2018 at 5:50
  • @josdev this will surely not work, the command which OP is using, is wrong Commented Jun 8, 2018 at 5:50

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Full command to run is /usr/bin/node /home/james/bot-accounts/Testing/botname/bot1.js /usr/bin/node /home/james/bot-accounts/Testing/botname1/bot2.js etc..... have updated my code to no luck. Been changing stuff so much and added in the preg_match as @zen suggested. Still learning php so makes it harder for me.
1

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.

Comments

0

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.

1 Comment

Worked awesome, Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.