我刚刚创建了一个脚本来检查进程是否处于事件状态,下面是我的 PHP 脚本。 Linux 服务器中的事件进程为 16269,但显示“进程已死亡”。有什么方法可以检查进程名称吗?
<?php
$pid = $_POST["pid"] ;
function checkPid($pid)
{
// create our system command
$cmd = "ps $pid";
// run the system command and assign output to a variable ($output)
exec($cmd, $output, $result);
// check the number of lines that were returned
if(count($output) >= 2){
// the process is still alive
echo '<h1> Process is running </h1>';
echo '<img src="/proview/green.gif" width="16" height="16" alt="Process is Alive" />';
return true;
}
// the process is dead
echo '<h1> Process is dead </h1>';
echo '<img src="/proview/red.gif" width="16" height="16" alt="Process is dead"/>';
return false;
}
if(isset($_POST['submit']))
{
checkPid($pid);
}
?>
<body>
<p>
<form method="post" action="index.php">
<input type="text" name="pid">
<input type="submit" value="Check Processe by ID" name="submit">
</form>
</body>
最佳答案
尝试以下操作:
<?php
function checkPid($pid)
{
// returns something like:
// 8987 pts/0 00:00:00 bash
$result = exec("ps -A|grep {$pid}");
if(preg_match("/{$pid}/",$result))
{
echo "PID {$pid} is running.";
return true;
}
else
{
echo "PID {$pid} is NOT running.";
return false;
}
}
if( isset($_POST['submit']) && isset($_POST["pid"]) )
{
checkPid( $_POST["pid"] );
}
?>
<body>
<p>
<form method="post" action="index.php">
<input type="text" name="pid">
<input type="submit" value="Check Processe by ID" name="submit">
</form>
</body>
对我来说效果很好。祝你好运!
关于用于检查 Linux 进程是否处于事件状态的 PHP 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31995894/