如何在 Linux 中运行程序并知道它的 PID?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9890062/
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 run a program and know its PID in Linux?
提问by Suzan Cioc
How to run a program and know its PID in Linux?
如何在 Linux 中运行程序并知道它的 PID?
If I have several shells running each other, will they all have separate PIDs?
如果我有多个 shell 相互运行,它们是否都有单独的 PID?
采纳答案by l0b0
Greg's wikito the rescue:
格雷格的维基救援:
$!
is the PID of the last backgrounded process.kill -0 $PID
checks whether$PID
is still running. Only use this for processes started by the current process or its descendants, otherwise the PID could have been recycled.wait
waits for all children to exit before continuing.
$!
是最后一个后台进程的PID。kill -0 $PID
检查是否$PID
仍在运行。仅用于当前进程或其后代启动的进程,否则 PID 可能已被回收。wait
在继续之前等待所有孩子退出。
Actually, just read the link - It's all there (and more).
实际上,只需阅读链接 - 它就在那里(以及更多)。
$$
is the PID of the current shell.
And yes, each shell will have its own PID (unless it's some homebrewed shell which doesn't fork
to create a "new" shell).
是的,每个 shell 都有自己的 PID(除非它是一些不会fork
创建“新”shell 的自制shell)。
回答by Dirk Eddelbuettel
1) There is a variable for that, often $$
:
1)有一个变量,通常是$$
:
edd@max:~$ echo $$ # shell itself
20559
edd@max:~$ bash -c 'echo $$' # new shell with different PID
19284
edd@max:~$ bash -c 'echo $$' # dito
19382
edd@max:~$
2) Yes they do, the OS / kernel does that for you.
2) 是的,他们这样做了,操作系统/内核会为您做到这一点。
回答by Xander
the top command in linux(Ubuntu) shows the memory usage of all running programs in linux with their pid. Kill pid can kill the process.
linux(Ubuntu) 中的 top 命令显示了 linux 中所有正在运行的程序及其 pid 的内存使用情况。kill pid 可以杀死进程。