Tux

...making Linux just a little more fun!

Two-cent Tip: backgrounding the last stopped job without knowing its job ID

Mulyadi Santosa [mulyadi.santosa at gmail.com]


Mon, 22 Feb 2010 16:14:09 +0700

For most people, to send a job to background after stopping a task, he/she will take a note the job ID and then invoke "bg" command appropriately like below:

$ (while (true); do yes  > /dev/null ; done)
^Z
[2]+  Stopped                 ( while ( true ); do
    yes > /dev/null;
done )
 
$ bg %2
[2]+ ( while ( true ); do
    yes > /dev/null;
done ) &

Can we omit the job ID? Yes, we can. Simply replace the above "bg %2" with "bg %%". It will refer to the last stopped job ID. This way, command typing mistake could be avoided too.

-- 
regards,
 
Mulyadi Santosa
Freelance Linux trainer and consultant
 
blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com


Top    Back


Thomas Adam [thomas at xteddy.org]


Mon, 22 Feb 2010 09:28:43 +0000

On 22 February 2010 09:14, Mulyadi Santosa <mulyadi.santosa@gmail.com> wrote:

> For most people, to send a job to background after stopping a task,
> he/she will take a note the job ID and then invoke "bg" command
> appropriately like below:
>
> $ (while (true); do yes  > /dev/null ; done)
> ^Z
> [2]+  Stopped                 ( while ( true ); do
>    yes > /dev/null;
> done )

This is very shell-specific here in terms of output:

% xterm
^Z
zsh: suspended  xterm
> $ bg %2
> [2]+ ( while ( true ); do
>    yes > /dev/null;
> done ) &
>
> Can we omit the job ID? Yes, we can. Simply replace the above "bg %2"
> with "bg %%". It will refer to the last stopped job ID. This way,
> command typing mistake could be avoided too.

Precisely -- which is where the "jobs" builtin is also useful:

% jobs -p
[1]  - 5317 running    xterm
[2]  + 5452 running    xterm

-- Thomas Adam


Top    Back


Ben Okopnik [ben at linuxgazette.net]


Mon, 22 Feb 2010 09:52:41 -0500

On Mon, Feb 22, 2010 at 04:14:09PM +0700, Mulyadi Santosa wrote:

> For most people, to send a job to background after stopping a task,
> he/she will take a note the job ID and then invoke "bg" command
> appropriately like below:
> 
> $ (while (true); do yes  > /dev/null ; done)
> ^Z
> [2]+  Stopped                 ( while ( true ); do
>     yes > /dev/null;
> done )
> 
> $ bg %2
> [2]+ ( while ( true ); do
>     yes > /dev/null;
> done ) &
> 
> Can we omit the job ID? Yes, we can. Simply replace the above "bg %2"
> with "bg %%". It will refer to the last stopped job ID. This way,
> command typing mistake could be avoided too.

What's wrong with a simple 'bg'? The default, when you don't specify an argument, is the current job.

In my experience, specifying an arg to 'bg' or 'fg' is unnecessary 99% of the time - but that may just be the way that I use the job system, since I rarely have more than one thing backgrounded at any one time. The only place where I find '%%' useful is when it's coupled with 'kill': sometimes, a process that's not amenable to being stopped with 'Ctrl-C' will respond to a 'Ctrl-Z', after which it can be killed off with 'kill %%'.

-- 
* Ben Okopnik * Editor-in-Chief, Linux Gazette * http://LinuxGazette.NET *


Top    Back


Mulyadi Santosa [mulyadi.santosa at gmail.com]


Tue, 23 Feb 2010 00:09:13 +0700

Hi Ben...

On Mon, Feb 22, 2010 at 9:52 PM, Ben Okopnik <ben@linuxgazette.net> wrote:

> What's wrong with a simple 'bg'? The default, when you don't specify an
> argument, is the current job.

Oh, you're right.....thanks, I just realized that after trying it by myself. Fun, wish I knew it from long time ago :)

-- 
regards,
 
Mulyadi Santosa
Freelance Linux trainer and consultant
 
blog: the-hydra.blogspot.com
training: mulyaditraining.blogspot.com


Top    Back