1837271 Members
3004 Online
110115 Solutions
New Discussion

related process

 
ust3
Regular Advisor

related process

I have the below script that kill the process in the file /tmp/process.list

for i in `ls -l /tmp/process.list`
do
kill $i
done

the file is as below.
$vi /tmp/process.list
58245
55874
5842
58745 < -- all these are process


now , I not only want to kill the process in this list , I also want kill the related process ( all its child and parent process ) , I know I can find the process by "ps -ef" , can advise how to add "ps -ef" to my above script so that the related process can be killed ?


thx
17 REPLIES 17
Yogeeraj_1
Honored Contributor

Re: related process

hi,

did you try:

pids=$(ps -ef | grep | awk '{print $2}')
kill -9 $pids


kind regards
yogeeraj
No person was ever honoured for what he received. Honour has been the reward for what he gave (clavin coolidge)
F Verschuren
Esteemed Contributor

Re: related process

ps -ef | grep | awk '{print $2,$3}' |while read line
kill -9 $pids

be carefull killing parents, is this realy wat you want?
F Verschuren
Esteemed Contributor

Re: related process

you can try this one if the echo looks like what you want you can remove it to thave the real kills...

for i in `ls -l /tmp/process.list`
do
for x in ` ps -ef | grep $i | awk '{print $2, $3}' `
do
echo kill $x
done
done
F Verschuren
Esteemed Contributor

Re: related process

one more safty to make sure you ps does not find a part of a proces numbre:

for i in `ls -l /tmp/process.list`
do
for x in ` ps -ef | grep " "$i" " | awk '{print $2, $3}' `
do
echo kill $x
done
done
Nitin Kumar Gupta
Trusted Contributor

Re: related process

Little modification

for psid in `cat /tmp/process.list`
do
for psids in `ps -ef | grep $psid | awk '{ print $2, $3 }'`
do
kill -9 $psids
done
done


Rgds
-NKG-
Kapil Jha
Honored Contributor

Re: related process

Some times parent dies and new parent of that proxesses become init processes.
So pls be care full while running this as this could kill ur init process.
And a small mistake could be fatel.
Or add "grep -v init" in above solutions.
BR,
Kapil
I am in this small bowl, I wane see the real world......
Dennis Handly
Acclaimed Contributor

Re: related process

>I know I can find the process by "ps -ef"

You can find the one process by: ps -fp PID

Kapil: So pls be careful while running this as this could kill your init process.

It won't let you cut your throat, kill(2):
pid can equal 1 unless sig is SIGKILL or SIGSTOP.

So it is init's job to handle the other signals.

>F Verschuren: one more safety to make sure your ps does not find a part of a process number:

Probably something like this:
for pid in $(< /tmp/process.list) ; do
echo kill $(UNIX95= ps -e -o pid= -o ppid= | grep -w $pid)
done

This won't kill any deeper than the child.
ust3
Regular Advisor

Re: related process

thx replies,

I think the below is fine , as your advice , I have two problem when kill the process , can help to advise the solution ,

1. if use the script to check other related process , I am afraid it will kill the process that init id is 1 or 0 , I think this is system process and should not be killed , can advise how to avoid this kind of process to be killed ? how to change the script to avoid kill this kind of process ?

2. As this script is run by user themself , if they killed process in /tmp/process.list , so they have killed the current login , so they can't kill the related process , can advise how to make sure they can kill the related process first ? Thx.


or psid in `cat /tmp/process.list`
do
for psids in `ps -ef | grep $psid | awk '{ print $2, $3 }'`
do
kill -9 $psids
done
done
Dennis Handly
Acclaimed Contributor

Re: related process

>1. if use the script to check other related process, I am afraid it will kill the process that init id is 1 or 0

I mentioned this can't happen for init. (You might want to test this on a test system first. :-)

>2. As this script is run by user themselves, ..., can advise how to make sure they can kill the related process first?

Go back to my nohup suggestion:
http://forums12.itrc.hp.com/service/forums/questionanswer.do?threadId=1181073

Then take my suggestion and wrap it another $() and do one single kill:
nohup echo kill $(for pid in $(< /tmp/process.list) ; do
UNIX95= ps -e -o pid= -o ppid= | grep -w $pid
done)

Remove echo after you have checked it. You can check by:
$ xargs -n1 ps -fp < nohup.out | grep -v COMMAND | sort -u
(Ignore "ps: wrong PID number kill".)

(Remove echo after you have checked it.)
ust3
Regular Advisor

Re: related process

thx reply ,

Sorry to my stupid , the below script is OK , but I still have question , the psid is only child process of pid , so from the below result , $3 is the pid ( 31694)

the script :
or psid in `cat /tmp/process.list`
do
for psids in `ps -ef | grep $psid | awk '{ print $2, $3 }'`
do
kill -9 $psids
done
done

the result
31697 31694
1548 29714

but what I want to kill is ppid , I can find it by ps -ef | grep 31694 | awk '{ print $2, $3 }'` , this $3 is the ppid , if I want to kill this $3 , can advise how can I change the script ? Thx
ust3
Regular Advisor

Re: related process

Actually , I want to kill BOT pid and ppid . Thx
ust3
Regular Advisor

Re: related process

sorry , I mean BOTH pid and ppid
Dennis Handly
Acclaimed Contributor

Re: related process

>but I still have question, the psid is only child process of pid, so from the below result, $3 is the pid (31694)

You're going to have to have a picture. You need to explain why you think your script doesn't work.

Your script will kill the list of PIDs. And it will also kill their children and their parent.

It does that by first finding PID in PID field, and then killing both PID and PPID. If it finds in in the PPID field, it kills child and the PID again. (I assumed you didn't care that you killed some twice.)

So since I think it does what you want, you will need to explain what's missing by an example.

Note: should really be using my for/UNIX95= ps solution that correctly "greps" and even Bill will be happy. :-)

Basically use my script and that xargs checking script.
ust3
Regular Advisor

Re: related process

thx reply,

You're going to have to have a picture. You need to explain why you think your script doesn't work.
--> the reason is the /tmp/process.list , the process id (58245,55874,5842,58745) in this file is not a pid , I can use the script to find the pid , and now want to have another script to find the ppid , the /tmp/process.list is generated from the another script .
Dennis Handly
Acclaimed Contributor

Re: related process

>the reason is the /tmp/process.list, the process id (58245,55874,5842,58745) in this file is not a pid

PIDs are PIDs. :-)

>now want to have another script to find the ppid

To find a list of PPIDs for each PID in that file:
for pid in $(< /tmp/process.list); do
UNIX95= ps -p $pid -o ppid=
done
ust3
Regular Advisor

Re: related process

thx reply again,

Your script can find the ppid , it is OK.

for pid in $(< /tmp/process.list); do
UNIX95= ps -p $pid -o ppid=
done

But if I want to find the parent id of this ppid , can advise what can i do ? thx

ps. what I actucally want is the parent id of ppid in the /tmp/process.list .
Dennis Handly
Acclaimed Contributor

Re: related process

>But if I want to find the parent id of this ppid, can advise what can i do?

>what I actually want is the parent id of ppid in the /tmp/process.list.

The file just has PIDs. I find the PPID of each PID in that file.

If you really want to go back one more:
for pid in $(< /tmp/process.list); do
UNIX95= ps -p $(UNIX95= ps -p $pid -o ppid=) -o ppid=
done