是否有命令行选项的组合ps
或pgrep
或其他一些比较直接的方法来确定特定的进程名是实际运行(可正常使用)..
通过“运行”,我的意思是专门排除<defunct>
正在运行的进程或任何其他非运行进程(例如僵尸:)...
此示例脚本显示了一个<defunct>
项目示例:
#!/bin/bash ubuntu 10.04
pgrep ^gnuserv$
# 25591
# 25599
# 27330
ps $(pgrep ^gnuserv$) # command ammended as per pilcrow's good suggestion
# PID TTY STAT TIME COMMAND
# 25591 ? Zs 0:00 [gnuserv] <defunct>
# 25599 ? Zs 0:00 [gnuserv] <defunct>
# 27330 pts/2 S+ 0:00 gnuserv
Run Code Online (Sandbox Code Playgroud)
我可以进一步sed
输出,但我认为/希望有更直接的方法......
小智 6
在您的评论中,您澄清:
我实际上正在寻找 ps 或 pgrep (或类似的)的单步选项,它只输出“活动”进程......
恐怕您对当前的 ps/pgrep 实现不走运。
像这样的后过滤依赖于对初始输出的充分理解,我没有......
但是,你可以得到理解和更好的是,该输出所需的控制。尝试这样的事情:
function pgrep_live {
pids=$(pgrep "$1");
[ "$pids" ] || return;
ps -o s= -o pid= $pids | sed -n 's/^[^ZT][[:space:]]\+//p';
}
Run Code Online (Sandbox Code Playgroud)
这将返回与您的输入字符串匹配的任何 pgrep 进程的 pid,这些进程“可用于正常使用”,即既不是死+未收割(Z)也不是停止(T)。