I have a command that outputs as follows:

# lostjobs

user1   12983     1  0 Feb04 ?        00:00:00 dbr UT:msmenu
user1   18253     1  0 Feb09 ?        00:00:00 dbr UT:msmenu
user2   22337     1  0 Feb09 ?        00:00:00 dbr UT:msmenu
user3   7164   7123  0 06:52 pts/0    00:00:10 dbr UT:msmenu

I know I can grab the ones with what I want to kill (which are the ones with question marks) with:

# lostjobs | grep ?

what I need to know is how can I loop through the results of the second one and kill them by id (the second column). I am not great with writing scripts for linux, so go easy on me.

Thanks for any help.

link|improve this question
feedback

2 Answers

up vote 0 down vote accepted

A solution is:

kill `lostjobs | awk '{ if ( $6 == "?" ) print $2 }'`
link|improve this answer
and that will kill all of them? – Toby Joiner Feb 11 '11 at 16:30
That will execute: kill 12983 18253 22337. Try to prefix the command with echo to see a preview. – cYrus Feb 11 '11 at 16:32
ok, to kill the users, we have to use -9, so I assume I can just do kill -9 `lostjobs |... – Toby Joiner Feb 11 '11 at 16:39
Sure, add whatever option you like to kill. – cYrus Feb 11 '11 at 16:41
feedback

This should also work:

kill -9 `lostjobs | grep ? | awk {'print $2'}`
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.