如何获得后台进程的PID ?

时间:2022-05-30 16:44:45

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,而不是后台进程。

7 个解决方案

#1


413  

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.

您不能使用作业控制,因为这是一个交互式特性,并与控制终端绑定在一起。一个脚本不一定有一个终端,所以工作控制不一定是可用的。

#2


103  

You can use the jobs -l command to get to a particular jobL

您可以使用jobs -l命令来获得特定的jobL。

^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:

从帮助工作:

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

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

jobs -p is another option which shows just the PIDs.

p是另一个显示pid的选项。

#3


33  

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

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

这里是一个bash会话的样本记录(%1是指从工作中看到的后台进程的序号):

$ echo $$
3748

$ sleep 100 &
[1] 192

$ echo $!
192

$ kill %1

[1]+  Terminated              sleep 100

#4


19  

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

一个更简单的方法来杀死bash脚本的所有子进程:

pkill -P $$

The -P flag works the same way with pkill and pgrep - it gets child processes, only with pkill the child processes get killed and with pgrep child PIDs are printed to stdout.

p标记与pkill和pgrep的工作方式相同——它获取子进程,只在pkill中杀死子进程,并将pgrep子pid打印到stdout中。

#5


3  

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.sh with proper permissions of course

然后运行它:./bgkill。当然要有适当的权限。

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

#6


2  

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所拥有的进程。它还显示了叉子。

#7


1  

pgrep can 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。所以,如果你想要一个自动清理的脚本,这应该可以做到:

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

#1


413  

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.

您不能使用作业控制,因为这是一个交互式特性,并与控制终端绑定在一起。一个脚本不一定有一个终端,所以工作控制不一定是可用的。

#2


103  

You can use the jobs -l command to get to a particular jobL

您可以使用jobs -l命令来获得特定的jobL。

^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:

从帮助工作:

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

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

jobs -p is another option which shows just the PIDs.

p是另一个显示pid的选项。

#3


33  

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

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

这里是一个bash会话的样本记录(%1是指从工作中看到的后台进程的序号):

$ echo $$
3748

$ sleep 100 &
[1] 192

$ echo $!
192

$ kill %1

[1]+  Terminated              sleep 100

#4


19  

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

一个更简单的方法来杀死bash脚本的所有子进程:

pkill -P $$

The -P flag works the same way with pkill and pgrep - it gets child processes, only with pkill the child processes get killed and with pgrep child PIDs are printed to stdout.

p标记与pkill和pgrep的工作方式相同——它获取子进程,只在pkill中杀死子进程,并将pgrep子pid打印到stdout中。

#5


3  

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.sh with proper permissions of course

然后运行它:./bgkill。当然要有适当的权限。

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

#6


2  

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所拥有的进程。它还显示了叉子。

#7


1  

pgrep can 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。所以,如果你想要一个自动清理的脚本,这应该可以做到:

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