6

Say I have backgrounded a process e.g. VIM with ctrl-z. Then I start an xterm using the following command xterm &> /dev/null &. If I just type disown then I will lose my vim process. I instead have to type the PID of the xterm process to disown it.

Is there any way to get round this or is there a reason this happens?

1 Answer 1

7

How to fix (short version)

You have couple of ways to do it:

  • With %- you will ask to disown the last background job.

    xterm &> /dev/null & disown %-
    
  • With $! instead you will ask to disown the last executed job (through its PID).

    xterm &> /dev/null & disown $!
    

So it will require to write a couple of characters more, but at least you can choose which ones :-)

The reasons

The reason is all here: when you execute a job sending it to background with &, essentially it doesn't change the current job, but only the previous one. The current job remains the one that was before.

Some words more and an example

You can check what said above with the command jobs, comparing its output before and after to have launched a job in background.

  • Before: let's suppose you have a couple of background processes just running from a terminal (e.g. launched with dolphin & and kate &).
    Now you start vim and you suspend it with Ctrl-Z.
    The output of jobs will be:

    [1]   Running                 dolphin . &
    [2]-  Running                 kate  &
    [3]+  Stopped                 vim
    

    vim is the current job, flagged with a +, and kate is the previous one, flagged with a -.

    Indeed on the bash reference manual, chapter 7.1 [1] is explained that
    " In output pertaining to jobs (e.g., the output of the jobs command), the current job is always flagged with a ‘+’, and the previous job with a ‘-’."

  • After: Finally you write your command xterm &> /dev/null & (without disown).

    Asking to jobs, this time it will answer:

    [1]   Running                 dolphin .
    [2]   Running                 kate 
    [3]+  Stopped                 vim
    [4]-  Running                 xterm &> /dev/null &
    

    Note that vim is still flagged with a + and xterm with a -.
    The last background job marked with - is xterm and not any more kate &.

  • In other words the system acts as if

    • First it runs xterm: the new command (xterm) becomes the current one and vim the previous one.
    • Then xterm is sent in background and the control comes back to the previous one in the list (vim with its status) that becomes again the current one and it is marked again with +. xterm instead is the new previous job and it is marked with a -.
    • At the end you find updated only the previous job (-) and not the current one (+).

Since you can call disown followed by a job number or by a PID you have the two variants

  • %-: the previous job may be referenced using %-, maybe more easy to remember if you think that also in jobs is marked with a -

  • $!: the PID of the last job that runs in background is in the special parameter $! [2].

The command disown will accept both.

References

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .