I run physics analysis jobs in a computer grid. Sometime the jobs go wrong and I have to kill them (one by one) which is painful. Can you please suggest me how can I select only the numeric values (748736838 and so on) from this output:

$ps -W

mhaque -748736838   0   W                                   
mhaque -748736879   0   W                                   
mhaque -748737079   0   W                                   
mhaque -748737185   0   W                                   
mhaque -748737276   0   W

(and hundred of lines like this)

I tried few sed/awk/grep command (from stackExchange) but could not separate the numeric values. Is there command which can select the numeric values and also place 'kill' in front of them? For example something like this (piping):

ps -W  |  awk/sed/grep (what_to_use) |  (some_command to place "kill") | > file.list

Which would give me the following (in file.list):

kill 748736838 
kill 748736879
...so on..

Then I can simply copy paste it in the grid shell to kill all offending/long waiting jobs.


If you want to kill all of the mhaque jobs, it is even simpler to do:

pkill -9 mhaque

(-9 is the signal to send, in this case I chose SIGKILL, change it accordingly to your needs).


pidof mhaque 

returns the simple list of the jobs PIDs, with nothing else. You can use it also as follows

kill -9 $(pidof mhaque)

which produces the same effect as the first command. All of this is much easier than scripting. For instance,

$ vlc fulvio.mkv &> /dev/null &
[1] 28006
$ vlc fulvio.mkv &> /dev/null &
[2] 28028
$ pidof vlc
28028 28006
$ pkill -9 vlc
$ ps ax | grep vlc
28054 pts/18   S+     0:00 grep --color=auto vlc
[1]-  Killed                  vlc fulvio.mkv &> /dev/null
[2]+  Killed                  vlc fulvio.mkv &> /dev/null


in reply to the clarification contained in the comment, the command

ps -W | awk '{print $2}' | cut -d"-" -f2

produces the simple list of jobs'PIDs. You can write them to a file, or process them directly as

kill -9 $(ps -W | awk '{print $2}' | cut -d"-" -f2)


I am a bit surprised this does not work as expected, because the above command is just standard Bash. Which system are you on, exactly? At any rate, you may try:

 for i in $(ps -W | awk '{print $2}' | cut -d"-" -f2); do kill -9 $i; done
  • Hi the pkill, pidof is not working in the grid. Our grid has customised ps, pid etc, 'alien_ps', 'alien_pid'. so, alien_ps -W gives me the job list that is in Waiting: mhaque -748736838 0 W . the W and 0 is in blue and 0 is blinking. somehow this command (one of colleague tole me) works: alien_ps | grep -v - | awk '{print "kill " $2}' But I don't understand the work of commands. Can you please explain how grep and awk is performing the task? – R. Haq Oct 17 '16 at 9:52
  • Hi Marius, thanks for the command. Its working but when I try to print a kill before each jod id, it just prints one 'kill' at the end of the list: 748830099 748831389 748831400 748831664 748831704 kill – R. Haq Oct 17 '16 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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