我有几个屏幕会话,每个会话都运行一个 java 应用程序。
例如,我有以下 3 个屏幕会话:
7361.ARM48 (Detached)
7397.ROP90 (Detached)
7314.ARM22 (Detached)
这是 ps -A 的输出
7314 ? 00:00:00 screen
7329 pts/1 00:00:16 java
7361 ? 00:00:00 screen
7375 pts/2 00:00:02 java
7397 ? 00:00:00 screen
7411 pts/3 00:00:02 java
有没有办法可靠地检测每个屏幕会话是否正在运行其 java 应用程序?
我查看了 Linux 的 screen 手册,但没有找到任何可以显示 screen 会话子进程的 screen 命令(http://linux.die.net/man/1/screen)
答案1
这听起来像是
ps fx
并且它还适用于一个屏幕会话中的多个窗口
答案2
有多种选择,输出ps aux
可以按终端名称和 pid 排序。这将为您提供每个屏幕的每个子进程的列表,但不会清楚地将(pts/N)每个屏幕与其内部的进程关联起来:
例如
$ ps aux | sort -k7.2r |
grep -Ei 'pts|java|screen' |
awk '{ printf "%10s %6s %6s %6s %s\n", $1, $2, $7, $9, $11}'
jaroslav 30700 tty3 Apr07 screen
root 4933 ? Mar16 SCREEN
jaroslav 5024 ? Mar12 SCREEN
root 3811 ? Apr07 sshd:
jaroslav 3716 ? Apr07 SCREEN
jaroslav 11996 ? Apr05 SCREEN
jaroslav 11139 ? Apr05 sshd:
root 4934 pts/8 Mar16 -/bin/bash
root 3819 pts/6 Apr07 -bash
jaroslav 3718 pts/5 Apr07 -/bin/bash
jaroslav 3810 pts/5 Apr07 ssh
jaroslav 11998 pts/2 Apr05 -/bin/bash
jaroslav 11140 pts/1 Apr05 -bash
jaroslav 12594 pts/1 13:52 sort
jaroslav 12595 pts/1 13:52 grep
jaroslav 12596 pts/1 13:52 awk
jaroslav 12593 pts/1 13:52 ps
jaroslav 5041 pts/0 Mar12 /bin/bash
jaroslav 5076 pts/0 Mar12 /usr/lib/jvm//sun-jdk-1.6/bin/
或者,您可以pstree
在每个屏幕上运行以列出其子项。这可能就是您想要的。
$ echo $(pidof screen;pidof SCREEN)| tr ' ' \\n |
xargs -L1 pstree -lanu
creen,jaroslav -dR serv
screen,jaroslav -dR java3
`-bash
`-java -cp /home/jaroslav/src/java/ TestUlimit
`-11*[{java}]
screen,jaroslav -dR java2
`-bash
`-java -cp /home/jaroslav/src/java/ TestUlimit
`-11*[{java}]
screen,jaroslav -dR java1
`-bash
`-java -cp /home/jaroslav/src/java/ TestUlimit
`-11*[{java}]
screen,jaroslav -dR serv
`-bash
screen,jaroslav -dmS nailgun /home/jaroslav/bin/nailgun
`-nailgun /home/jaroslav/bin/nailgun
`-java -server -jar /usr/share/nailgun/lib/nailgun.jar localhost:64781
`-16*[{java}]
screen -dR emr
`-bash
screen,jaroslav -dR gdb
`-bash
`-ssh l00
或者,找到每个 java 进程的父进程。
$ for i in `pidof java`; do pstree -sp $i ;done | grep -v '^ '
init(1)---screen(13097)---bash(13099)---java(13943)-+-{java}(13947)
init(1)---screen(13004)---bash(13006)---java(13927)-+-{java}(13931)
init(1)---screen(12911)---bash(12913)---java(13911)-+-{java}(13915)
init(1)---screen(5024)---nailgun(5041)---java(5076)-+-{java}(5095)