简体   繁体   中英

Check if process is running with PHP

I'm trying to figure out if a process is running in PHP by using pgrep inside of exec().

checkstatus.php:

<?php
echo exec("pgrep myProgram");
?>

health.js:

$(document).ready(function() {
  $.post("checkstatus.php", {},
    function(data) {
      alert(data);
    }
  );
});

From the command line, this works: php checkstatus.php

Which retrns the PID

The above code should work, but is returning null. Is there an error that I'm overlooking/a better way to do this?

try this (untested):

$(document).ready(function() {);
$.post( "checkstatus.php", function( data ) {
alert( "Data Loaded: " + data );
});
});

and try shell_exec checkstatus.php:

<?php
echo shell_exec("pgrep myProgram");
?>

And make sure to have the following settings for php and files.

    **safe mode off
    full read/write/exec permissions
    displaying all errors
    no disabled functions**

From your response to Class, checkstatus.php is returning null if executed directly. What is the output of pgrep myProgram inside the command line?

The following code is functional on WAMP running on Windows 7:

health.html

<!doctype html>
<head>
<title></title>
<script src="jquery-2.1.1.min.js"></script>
<style>
#output {
    background-color: #ddd;
    margin-top: 5px;
}
</style>
</head>
<body>
<button id="click">Click Me</button>
<div id="output"></div>
</body>
<script>
$("#click").click(function()
{
    console.log('clicked');
    $.post("health.php",
        function(data) {
          $('#output').html('From health.php: ' + data);
        }
    );
});
</script>
</html>

health.php

<?php

//system('ver');
echo exec('ver');

?>

This work for me.

checkstatus.php:

<?php
echo exec("pgrep dropbox");

health.php

<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Test</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <!-- javascript/jQuery -->
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>

</head>
<body>
<script>
    $(document).ready(function () {
        $.post("checkstatus.php", {},
            function (data) {
                alert(data);
            }
        );
    });
</script>
</body>
</html>

Screenshot: 在此输入图像描述

I think the issue you may be having may be with linux permissions , more a linux issue than PHP?

PHP runs under a separate user account to you. (try the whoami command) On my system, the username is apache instead of my normal user account for the PHP and apache programs. Try this:

<?php
    var_dump( shell_exec( 'whoami' ) );

I used var_dump instead of echo so we can see any non-scalar data types that may be returned where required ( See here ).

Now... What your issue most likely is, is that when running a process list from a non-root account, it will only give you the list of processes associated with your account. I ran across this issue when I was creating a system monitoring application for work. If you do a process list from root or if you use sudo, you should theoretically get every process.

The solution, however, is NOT to sudo your webserver account - this would be a major security flaw. The solution is to find another way to do what you want to do. For me, I was able to create a bash script triggered every minute by a cron job that would grab all the information I required and save it in JSON format to a file accessible by the web server.

What, exactly, is it that you are trying to achieve?

EDIT: I've noticed in response to another answer that you are getting results when invoking the script from PHP in terminal. This is because you will be running PHP with the same permissions as your user account, meaning PHP is running the pgrep command as your user account. You should see exactly the same results from PHP that you would from manually running it.

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). Get it here: https://github.com/merlinthemagic/MTS

After downloading you would simply use the following code:

//Setting the second argument in getShell():
//true will return a shell with root
//false will return a shell with the php execution user
$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('pgrep myProgram');

$return1 holds a string with the return from bash.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM