11.6. Stopped Jobs Count
Torben Fjerdingstad (<tfj at fjerdingstad dot dk>) wrote to
tell me that he often stops jobs and then forgets about them. He uses his
prompt to remind himself of stopped jobs. Apparently this is fairly
popular, because as of Bash 2.04, there is a standard escape sequence for
jobs managed by the shell:
[giles@zinfandel]$ export PS1='\W[\j]\$ '
giles[0]$ man ls &
[1] 31899
giles[1]$ xman &
[2] 31907
[1]+ Stopped man ls
giles[2]$ jobs
[1]+ Stopped man ls
[2]- Running xman &
giles[2]$ |
Note that this shows both stopped and running jobs. At the console, you
probably want the complete count, but in an xterm you're probably only
interested in the ones that are stopped. To display only these, you could
use something like the following:
[giles@zinfandel]$ function stoppedjobs {
-- jobs -s | wc -l | sed -e "s/ //g"
-- }
[giles@zinfandel]$ export PS1='\W[`stoppedjobs`]\$ '
giles[0]$ jobs
giles[0]$ man ls &
[1] 32212
[1]+ Stopped man ls
giles[0]$ man X &
[2] 32225
[2]+ Stopped man X
giles[2]$ jobs
[1]- Stopped man ls
[2]+ Stopped man X
giles[2]$ xman &
[3] 32246
giles[2]$ sleep 300 &
[4] 32255
giles[2]$ jobs
[1]- Stopped man ls
[2]+ Stopped man X
[3] Running xman &
[4] Running sleep 300 & |
This doesn't always show the stopped job in the prompt that follows
immediately after the command is executed - it probably depends on whether
the job is launched and put in the background before jobs
is run.
| There is a known bug in Bash 2.02 that causes the jobs
command (a shell builtin) to return nothing to a pipe. If you try the
above under Bash 2.02, you will always get a "0" back regardless of how
many jobs you have stopped. This problem is fixed in 2.03. |
Relative speed: 'jobs -s | wc -l | sed -e "s/ //g" ' takes about 0.24
seconds on an unloaded 486SX25.