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