如何在 bash 中列出所有后台 pid
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18867739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list all background pids in bash
提问by code_fodder
Either I am not able to phrase my search correctly or the answer is not easy to find!, but I am trying to figure out how to list all of my background task PIDs. For example:
要么我无法正确表达我的搜索,要么不容易找到答案!,但我正在尝试弄清楚如何列出我所有的后台任务 PID。例如:
So far I have found that to list the last PID we use:
到目前为止,我发现要列出我们使用的最后一个 PID:
$!
But now I want to list the PID of the task before that (if one exists), but I can't find how to do that. Utlimatly I want to list all my background task PIDs.
但是现在我想在此之前列出任务的PID(如果存在),但我找不到如何做到这一点。最终我想列出我所有的后台任务 PID。
I know we can also find last job ID with:
我知道我们还可以通过以下方式找到最后一个工作 ID:
%% (last job in list)
%1 (first job in list)
%2 (second job in list)
But the same does not seem to work for process id?
但是对于进程 id 似乎也不起作用?
Thanks all :)
谢谢大家:)
回答by Michael Kazarian
Use ps S
. For example:
使用ps S
. 例如:
$ vim &
[1] 8263
$ ipython &
[2] 8264
$ ps S
PID TTY STAT TIME COMMAND
3082 pts/0 Ss 0:00 bash
3137 pts/0 Sl+ 0:00 python /usr/bin/ipython
8207 pts/2 Ss 0:00 bash
8263 pts/2 T 0:00 vim
8264 pts/2 Tl 0:00 python /usr/bin/ipython
8284 pts/2 Tl 0:00 python /usr/bin/ipython
8355 pts/2 R+ 0:00 ps S
If you want get PIDs use below:
如果您想获取 PID,请使用以下方法:
$ ps S | awk '{print $ 1 }' | grep -E '[0-9]'
3082
3137
8207
8263
8264
8284
8357
8358
835
Also you can use jobs -l
But it show background processes only for current session.
您也可以使用jobs -l
但它仅显示当前会话的后台进程。
回答by cnicutar
But the same does not seem to work for process id?
但是对于进程 id 似乎也不起作用?
You can try jobs -l
or -p
. The -l
and -p
switches cause the jobs
command to also output process IDs.
你可以试试jobs -l
或-p
。的-l
和-p
原因的切换jobs
命令也输出进程ID。
回答by ghoti
In bash, as in tcsh, the command you probably want is jobs -l
(for Long).
在 bash 中,就像在 tcsh 中一样,您可能想要的命令是jobs -l
(对于 Long)。
[ghoti@pc ~]$ sleep 300 &
[1] 33811
[ghoti@pc ~]$ sleep 301 &
[2] 33812
[ghoti@pc ~]$ sleep 302 &
[3] 33813
[ghoti@pc ~]$ jobs -l
[1]- 33811 Running sleep 300 &
[2]- 33812 Running sleep 301 &
[3]+ 33813 Running sleep 302 &
[ghoti@pc ~]$
回答by Alfe
If you also want to see your child processes which aren't handled as a job by the shell anymore (e. g. because you disown
ed them deliberately or similar), then you can use this to find all processes which have youas their parent:
如果您还想查看不再被 shell 处理为作业的子进程(例如,因为您disown
故意或类似地编辑它们),那么您可以使用它来查找所有将您作为其父进程的进程:
grep "PPid:.*$$" /proc/[0-9]*/status | cut -d/ -f3
Also
还
ps --ppid $$
can be of use. (Credits to @Michael Kazarian who also has an answer here.)
可以用。(感谢@Michael Kazarian,他在这里也有答案。)