首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >linux上进程的状态

问linux上进程的状态
EN

Stack Overflow用户
提问于 2015-11-29 03:52:38
回答 1查看 14.9K关注 0票数 4

我在下面的数组中有一个pid的列表

代码语言:javascript
复制
All_Process_Pid

对于每个进程,我希望使用它的pid来检查进程的状态。

更确切地说,对于每个pid,我想知道它的对应过程是否是-

  1. 正在运行
  2. 完成
  3. 已停止

我怎么能在巴什做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-29 04:02:13

首先,man ps中的进程状态比“运行”、“完成”和“停止”更多。

代码语言:javascript
复制
PROCESS STATE CODES
    Here are the different values that the s, stat and state output 
    specifiers (header "STAT" or "S") will display to describe the
    state of a process:

       D    uninterruptible sleep (usually IO)
       R    running or runnable (on run queue)
       S    interruptible sleep (waiting for an event to complete)
       T    stopped by job control signal
       t    stopped by debugger during the tracing
       W    paging (not valid since the 2.6.xx kernel)
       X    dead (should never be seen)
       Z    defunct ("zombie") process, terminated but not reaped by its parent

您可以使用命令ps通过它的pid (进程ID)获取一个进程的状态。

代码语言:javascript
复制
ps -q <pid> -o state --no-headers

来自man ps

代码语言:javascript
复制
-q pidlist
     Select by PID (quick mode).  This selects the processes whose
     process ID numbers appear in pidlist.  With this option ps reads the
     necessary info only for the pids listed in the pidlist and doesn't
     apply additional filtering rules. The order of pids is unsorted and
     preserved. No additional selection options, sorting and forest type
     listings are allowed in this mode.  Identical to q and --quick-pid.

如果在bash脚本中有pid "All_Process_Pid“数组,那么只需:

代码语言:javascript
复制
for i in "${All_Process_Pid[@]}"
do
     echo "process $i has the state $(ps -q $i -o state --no-headers)"
done
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33979231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档