Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

A solution is:

kill `lostjobs | awk '{ if ( $6 == "?" ) print $2 }'`
share|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
add comment

This should also work:

kill -9 `lostjobs | grep ? | awk {'print $2'}`
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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