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

如何从我的shell脚本得到这个过程的PID ?就我所能看到的变量$!包含当前脚本的PID,而不是后台进程。


可以使用jobs -l命令访问指定的jobL

^Z
[1]+  Stopped                 guard

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

在本例中,PID为46841。

来自帮助工作:

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

jobs -p是另一个选项,它只显示pid。

$$是当前脚本的pid 美元!是上次后台进程的pid吗

下面是bash会话的示例记录(%1指从job中看到的后台进程的序数):

$ echo $$
3748

$ sleep 100 &
[1] 192

$ echo $!
192

$ kill %1

[1]+  Terminated              sleep 100

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

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

您不能使用作业控制,因为作业控制是一个交互特性,并且与控制终端绑定。脚本不一定要附加终端,因此作业控制不一定可用。

你也可以使用pstree:

pstree -p user

这通常为“user”提供所有进程的文本表示,而-p选项提供进程id。据我所知,这并不取决于进程是否属于当前shell。它还显示了分叉。

这就是我所做的。看看吧,希望能有所帮助。

#!/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

然后使用:./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

pgrep可以获取父进程的所有子pid。如前所述,$$是当前脚本的PID。所以,如果你想要一个脚本自己清理,这应该可以做到:

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

还有一种更简单的方法可以杀死bash脚本的所有子进程:

pkill -P $$

- p标志与pkill和pgrep的工作方式相同——它获取子进程,只有使用pkill时子进程被杀死,使用pgrep时子pid被打印到标准输出。

我在提供各种基础设施对象时多次遇到过这个问题。很多时候,您需要使用kubectl或临时端口转发临时代理。我发现超时命令是一个很好的解决方案,因为它允许我的脚本是自包含的,我可以确保进程将结束。我尝试设置小的超时,并重新运行脚本,如果我仍然需要它。

如果你在开始工作后不能直接获得PID,你也可以尝试这个,稍后获得PID:

foo &

# do some stuff and then

pid=$(ps -aux | grep foo | tr -s ' ' | cut -d\  -f2)
kill $pid

解释:

Ps获取所有进程的信息include . command Grep正在为您的命令进行筛选 Tr是去掉重复的空格进行切割 cut是让你得到带有PID的列(在这里是2)