PHP:获取命令的 LINUX PID [英] PHP: Get the LINUX PID of a command

查看:34
本文介绍了PHP:获取命令的 LINUX PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的 linux 服务器 (Ubuntu) 中运行一个命令.例如:

I'm running a command in my linux server (Ubuntu). For example:

screen -A -h 1500 -m -dmS test_command_one /home/SKY-users/SKY-001/./script

有什么办法可以得到这个后台进程的PID,屏幕名称是:test_command_one?

Is there any way to the PID of this background progress which screen name is: test_command_one?

ps 辅助 |grep test_command_one:

ps aux | grep test_command_one:

root      6573  8.1  2.4 271688 123804 pts/4   Ss+  Oct19   3:04 /home/SKY-users/SKY-001/./ ...

我想取回这个 PID:6573

I'd like to get back this PID: 6573

PHP:(简单)

<?php 
$output = shell_exec('sudo ps aux | grep test_command_one');
$array = explode("\n", $output);
echo '<pre>'.print_r($array, true).'</pre>';
?>

感谢您的帮助!

推荐答案

结合@WagnerVaz 的代码

By combining with code by @WagnerVaz

$mystring = "test_command_one";
exec("ps aux | grep 'screen .* $mystring' | grep -v grep | awk '{ print $2 }' | head -1", $out);
print "The PID is: " . $out[0];

说明

  • ps aux - 显示所有用户的进程和隐藏进程
  • grep - 仅过滤同一行中包含screen"和test_command_one"的行
  • grep -v - 从输出中删除我们正在执行的同一行,因为它也会被匹配
  • awk '{ print $2 }' - awk 将输入分成列并使用多个空格作为分隔符.第二列的打印内容
  • head -1 - 将输出限制为仅第一行.如果您有多个屏幕运行,则仅返回第一个 ID.
  • 这篇关于PHP:获取命令的 LINUX PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆