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.

The shell command jobs supports a -p option to just get the list of process ids. Is there an easy way to get just a list of job ids? (I know there is, but I am not really a shell guy (yet)).

This would be very handy for kill, as killing a job id instead of a process id seems to also kill all child processes.

share|improve this question
    
+1 for "(yet)" :) –  Limited Atonement Jun 7 '12 at 15:39
add comment

2 Answers

up vote 4 down vote accepted

You can do something like this:

# create some background jobs
sleep 10 & sleep 10 & sleep 10 & sleep 10 & 
jobs | awk -F '[][]' '{print "%" $2}'

This will return

%1
%2
%3
%4

which I assume is what you're after.

share|improve this answer
add comment

This would be very handy for kill, as killing a job id instead of a process id seems to also kill all child processes.

You can do this a different way.

When it prints the pid, you can kill it and all its children by killing its process group.

You do this by negating the pid, e.g.

$ cat &
[1] 21273
$ kill -- -21273

You need to write -- or else -pid is interpreted as a signal (e.g. like kill -9).

Example:

$ man ls &
[1] 22267

$ ps j | head -1; ps j | grep 22267
 PPID   PID  PGID   SID TTY      TPGID STAT   UID   TIME COMMAND
18968 22267 22267 18968 pts/2    22327 T      500   0:00 man ls
22267 22281 22267 18968 pts/2    22327 T      500   0:00 /bin/sh /usr/bin/nroff -mandoc -Tutf8
22267 22282 22267 18968 pts/2    22327 T      500   0:00 less
22281 22286 22267 18968 pts/2    22327 T      500   0:00 groff -mtty-char -Tutf8 -mandoc
22286 22287 22267 18968 pts/2    22327 T      500   0:00 troff -mtty-char -mandoc -Tutf8
22286 22288 22267 18968 pts/2    22327 T      500   0:00 grotty

$ kill -- -22267
[1]+  Terminated              man ls

$ ps j | grep 22267

$
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.