Operating System - HP-UX
1833059 Members
2676 Online
110049 Solutions
New Discussion

Kill processes that follow

 
SOLVED
Go to solution
tyro
New Member

Kill processes that follow

The file attached shows the status of multiple processes running on my machine.

I want to kill all the processes that are listed after the 'Marker Process', i.e. Process4, Process5, Process6.

For this I need to extract the process ID of the processes listed after the 'Marker Process'.

Can someone please suggest a way to extract those PIDs. I know that awk '{print $9}' will get me the PIDs, but I only want the ones listed after the 'Marker Process'.

The next line in my code would read:
kill -9 $killproc

where killproc has the process ids of the above mentioned processes.

Thanks
7 REPLIES 7
Peter Godron
Honored Contributor

Re: Kill processes that follow

Hi,
can you use the following to extract the data:
cat | sed -n "/MarkerProcess/,/^$/n;p"
This should extract all the lines from MarkerProcess onwards.
You can then awk the process id and you should be done.
Hope this solves the problem.
Regards
Keith Bryson
Honored Contributor
Solution

Re: Kill processes that follow

Hello there

This one line should work (/tmp/input is the output you enclosed in your posting):

cat /tmp/input | sed -n '/MarkerProcess/,//p' | grep -v "MarkerProcess" | awk {'print $9'} | xargs kill -9

I'm assuming here though that you will only have Processx lines after the MarkerProcess line? If not, you can enter another grep to ensure you only have Processx lines to kill:

cat /tmp/kfb.input | sed -n '/MarkerProcess/,//p' | grep -v "MarkerProcess" | grep "^Process" | awk {'print $9'}

All the best - Keith
Arse-cover at all costs
Keith Bryson
Honored Contributor

Re: Kill processes that follow

Sorry, that last line should have included the xargs command:

cat /tmp/input | sed -n '/MarkerProcess/,//p' | grep -v "MarkerProcess" | grep "^Process" | awk {'print $9'} | xargs kill -9

Have fun - Keith
Arse-cover at all costs
Steven E. Protter
Exalted Contributor

Re: Kill processes that follow

Might be able to feed the process id's to this gkill. You take out the two interactive steps, and it is an automated, process killer.

This is a very powerful tool. Read the code, be familiar with it.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
tyro
New Member

Re: Kill processes that follow

Hi Peter,
I was not able to follow what the line you entered does. Would really appreciate if you could explain what '/^$/n;p' does.

When I tried using it in my code, and redirected the output to another file, I got an output with the MarkerProcess missing.

But its also listing Process1, Process2 and Process3. I only want Process4, Process5 and Process6 (that follow after MarkerProcess) to get listed. How do I get rid of those ?

Thanks
tyro
New Member

Re: Kill processes that follow

Keith,
That line was beautiful. Its solved my problem in a jiffy.

Trouble is, now I really want to know how it works. :-)

cat /tmp/input | sed -n '/MarkerProcess/,//p' | grep -v "MarkerProcess" | awk {'print $9'} | xargs kill -9

Understood that the grep -v command helped us exclude 'MarkerProcess' from the search.

Why did you use the ',//p' ? What does it do ?

Google showed me that:
# To print only lines which match regular expression (emulates "grep")
sed -n '/regexp/p'

But sed -n '/MarkerProcess/p' did not work. It gave me a bad argument error. :-S Curious.

Thanks
tyro
New Member

Re: Kill processes that follow

Hi Steven,
Thanks for the gkill. Really appreciate the idea.

Keith made the moves really swift. :-))