Exit zsh, but leave running jobs open?
BashZshJobsBash Problem Overview
Just switched from bash to zsh.
In bash, background tasks continue running when the shell exits. For example here, dolphin
continues running after the exit
:
$ dolphin .
^Z
[1]+ Stopped dolphin .
$ bg
[1]+ dolphin . &
$ exit
This is what I want as the default behavior.
In contrast, zsh's behavior is to warn about running jobs on exit
, then close them if you exit
again. For example here, dolphin
is closed when the second exit
-command actually exits the shell:
% dolphin .
^Z
zsh: suspended dolphin .
% bg
[1] + continued dolphin .
% exit
zsh: you have running jobs.
% exit
How do I make zsh's default behavior here like bash's?
Bash Solutions
Solution 1 - Bash
Start the program with &!
:
dolphin &!
The &!
(or equivalently, &|
) is a zsh-specific shortcut to both background and disown the process, such that exiting the shell will leave it running.
Solution 2 - Bash
From the zsh documentation:
>HUP
>
>... In zsh, if you have a background job running when the shell exits, the shell will assume you want that to be killed; in this case it is sent a particular signal called SIGHUP
... If you often start jobs that should go on even when the shell has exited, then you can set the option NO_HUP
, and background jobs will be left alone.
So just set the NO_HUP
option:
% setopt NO_HUP
Solution 3 - Bash
I have found that using a combination of nohup
, &
, and disown
works for me, as I don't want to permanently cause jobs to run when the shell has exited.
nohup <command> & disown
While just &
has worked for me in bash
, I found when using only nohup
, &
, or disown
on running commands, like a script that calls a java run command, the process would still stop when the shell is exited.
nohup
makes the command ignoreNOHUP
andSIGHUP
signals from the shell&
makes the process run in the background in a subterminaldisown
followed by an argument (the index of the job number in your jobs list) prevents the shell from sending aSIGHUP
signal to child processes. Usingdisown
without an argument causes it to default to the most recent job.
I found the nohup
and disown
information at this page, and the &
information in this SO answer.
Solution 4 - Bash
I typically use screen
for keeping background jobs running.
1) Create a screen session:
screen -S myScreenName
2) Launch your scripts,services,daemons or whatever
3) Exit (detach) screen-session with
screen -d
or
shortcut ALT+A then d
After few hundreds of years - if you want to resume your session (reattach):
screen -r myScreenName
If you want to know if there's a screen-session, its name and its status (attached or detached):
screen -ls
This solution works on all terminal interpreters like bash, zsh etc.
See also man screen