0

Geeks, I seek your help.

In my bash script I will need to kill a process and all its children - so, given a parent pid(2288 in this case) & I need to use that parent pid to find child PIDs, put them in a list and then kill them. I have tried the code below…

pstree -p | grep 2288

…but its output has child PIDs embeded within alphabetic characters, as you can see below…

S90service(2288)—-bash(2290)—-php(2291)

Is there a command, or programmatic way I can use to extract PIDs, and put them in a space-separated list ??

Please note that at present I am using…

(1)

pgrep -d’ ‘ -f partial-name-of-process

…to put the PIDs of the parent and its children into a space-separated list, and then kill them. I want to abandon this because of the real possibility of accidentally killing other processes whose names may match partial-name-of-process, and,

(2)

I have tried various other suggested techniques such as regex expressions with awk ‘printf $1’ etc, to extract PIDs from the output of pstree or ps, but most of these suggestions only work on the command line and fail when put to work inside a script. I will be very appreciative and thankful in advance if some geek somewhere can help with a simple technique to extract child PIDs for a given parent pid, and put them into a space-separated list. (My scripts are in bash, on Centos)

lazyone
  • 1
  • 3
  • You don't need to look up the child PIDs to kill them. Just kill the whole process group. – Charles Duffy Nov 27 '21 at 23:50
  • Or, better, start the processes in a cgroup and kill the whole cgroup. (Running them with systemd will do the former automatically, and let you kill things by service identifier, likewise letting you skip any need to mess with PIDs) – Charles Duffy Nov 27 '21 at 23:53
  • `S90service` implies that you're starting your service via rc.d (aka legacy sysvinit scripts); that's been considered obsolete in CentOS for many, _many_ releases now. The modern replacement is systemd services; once you have a `yourservice.service` defined, it can be stopped (with all its subprocesses!) with `systemctl stop yourservice`, and started/restarted/etc similarly. – Charles Duffy Nov 27 '21 at 23:57

0 Answers0