Linux 如何获取后台进程的进程ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1908610/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:36:29  来源:igfitidea点击:

How to get process ID of background process?

linuxshellbackground-processpid

提问by Volodymyr Bezuglyy

I start a background process from my shell script, and I would like to kill this process when my script finishes.

我从我的 shell 脚本启动了一个后台进程,我想在我的脚本完成时终止这个进程。

How to get the PID of this process from my shell script? As far as I can see variable $!contains the PID of the current script, not the background process.

如何从我的 shell 脚本中获取这个进程的 PID?据我所知,变量$!包含当前脚本的 PID,而不是后台进程。

采纳答案by camh

You need to save the PID of the background process at the time you start it:

您需要在启动时保存后台进程的PID:

foo &
FOO_PID=$!
# do other stuff
kill $FOO_PID

You cannot use job control, since that is an interactive feature and tied to a controlling terminal. A script will not necessarily have a terminal attached at all so job control will not necessarily be available.

您不能使用作业控制,因为这是一个交互功能并绑定到控制终端。脚本不一定有终端连接,因此作业控制不一定可用。

回答by jldupont

You can use the jobs -lcommand to get to a particular jobL

您可以使用该jobs -l命令获取特定作业L

^Z
[1]+  Stopped                 guard

my_mac:workspace r$ jobs -l
[1]+ 46841 Suspended: 18           guard

In this case, 46841 is the PID.

在这种情况下,46841 是 PID。

From help jobs:

来自help jobs

-l Report the process group ID and working directory of the jobs.

-l 报告作业的进程组 ID 和工作目录。

jobs -pis another option which shows just the PIDs.

jobs -p是另一种仅显示 PID 的选项。

回答by catwalk

  • $$is the current script's pid
  • $!is the pid of the last background process
  • $$是当前脚本的pid
  • $!是最后一个后台进程的pid

Here's a sample transcript from a bash session (%1refers to the ordinal number of background process as seen from jobs):

这是 bash 会话的示例记录(%1指从 中看到的后台进程的序号jobs):

$ echo $$
3748

$ sleep 100 &
[1] 192

$ echo $!
192

$ kill %1

[1]+  Terminated              sleep 100

回答by villaa

You might also be able to use pstree:

您也许还可以使用 pstree:

pstree -p user

This typically gives a text representation of all the processes for the "user" and the -p option gives the process-id. It does not depend, as far as I understand, on having the processes be owned by the current shell. It also shows forks.

这通常为“用户”提供所有进程的文本表示,并且 -p 选项提供进程 ID。据我所知,它不依赖于让当前 shell 拥有进程。它还显示叉子。

回答by Luis Ramirez

this is what I have done. Check it out, hope it can help.

这就是我所做的。看看吧,希望能帮到你。

#!/bin/bash
#
# So something to show.
echo "UNO" >  UNO.txt
echo "DOS" >  DOS.txt
#
# Initialize Pid List
dPidLst=""
#
# Generate background processes
tail -f UNO.txt&
dPidLst="$dPidLst $!"
tail -f DOS.txt&
dPidLst="$dPidLst $!"
#
# Report process IDs
echo PID=$$
echo dPidLst=$dPidLst
#
# Show process on current shell
ps -f
#
# Start killing background processes from list
for dPid in $dPidLst
do
        echo killing $dPid. Process is still there.
        ps | grep $dPid
        kill $dPid
        ps | grep $dPid
        echo Just ran "'"ps"'" command, $dPid must not show again.
done

Then just run it as: ./bgkill.shwith proper permissions of course

然后只需将其运行为:./bgkill.sh当然具有适当的权限

root@umsstd22 [P]:~# ./bgkill.sh
PID=23757
dPidLst= 23758 23759
UNO
DOS
UID        PID  PPID  C STIME TTY          TIME CMD
root      3937  3935  0 11:07 pts/5    00:00:00 -bash
root     23757  3937  0 11:55 pts/5    00:00:00 /bin/bash ./bgkill.sh
root     23758 23757  0 11:55 pts/5    00:00:00 tail -f UNO.txt
root     23759 23757  0 11:55 pts/5    00:00:00 tail -f DOS.txt
root     23760 23757  0 11:55 pts/5    00:00:00 ps -f
killing 23758. Process is still there.
23758 pts/5    00:00:00 tail
./bgkill.sh: line 24: 23758 Terminated              tail -f UNO.txt
Just ran 'ps' command, 23758 must not show again.
killing 23759. Process is still there.
23759 pts/5    00:00:00 tail
./bgkill.sh: line 24: 23759 Terminated              tail -f DOS.txt
Just ran 'ps' command, 23759 must not show again.
root@umsstd22 [P]:~# ps -f
UID        PID  PPID  C STIME TTY          TIME CMD
root      3937  3935  0 11:07 pts/5    00:00:00 -bash
root     24200  3937  0 11:56 pts/5    00:00:00 ps -f

回答by errant.info

pgrepcan get you all of the child PIDs of a parent process. As mentioned earlier $$is the current scripts PID. So, if you want a script that cleans up after itself, this should do the trick:

pgrep可以获得父进程的所有子PID。前面提到的$$是当前脚本的PID。因此,如果您想要一个自行清理的脚本,这应该可以解决问题:

trap 'kill $( pgrep -P $$ | tr "\n" " " )' SIGINT SIGTERM EXIT

回答by Alexey Polonsky

An even simpler way to kill all child process of a bash script:

一种更简单的方法来终止 bash 脚本的所有子进程:

pkill -P $$

The -Pflag works the same way with pkilland pgrep- it gets child processes, only with pkillthe child processes get killed and with pgrepchild PIDs are printed to stdout.

-P标志与pkilland以相同的方式工作pgrep- 它获取子进程,只有pkill子进程被杀死并且pgrep子 PID 被打印到标准输出。