Just switched from bash to zsh.
In bash, background tasks continue running when the shell exits. For example here,dolphin continues running after theexit:
$ dolphin .^Z[1]+ Stopped dolphin .$ bg[1]+ dolphin . &$ exitThis is what I want as the default behavior.
In contrast, zsh's behavior is to warn about running jobs onexit, then close them if youexit again. For example here,dolphin is closed when the secondexit-command actually exits the shell:
% dolphin .^Zzsh: suspended dolphin . % bg[1] + continued dolphin . % exitzsh: you have running jobs. % exitHow do I make zsh's default behavior here like bash's?
4 Answers4
Start the program with&!:
dolphin &!The&! (or equivalently,&|) is a zsh-specific shortcut to bothbackgroundanddisown the process, such that exiting the shell will leave it running.
11 Comments
setopt nohup in your~/.zshrc. Then starting a command backgrounded with& will also always disown it. Though by default zsh will still remind you that you have running jobs when you attempt to exit. You can turn that off too withsetopt nocheckjobs.alias z="sudo sleep 1 &|". Construct minimal examples like this, and you'll narrow it down eventually.From thezsh 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 optionNO_HUP, and background jobs will be left alone.
So just set theNO_HUP option:
% setopt NO_HUP2 Comments
I have found that using a combination ofnohup,&, anddisown works for me, as I don't want to permanently cause jobs to run when the shell has exited.
nohup <command> & disownWhile just& has worked for me inbash, I found when using onlynohup,&, ordisown on running commands, like a script that calls a java run command, the process would still stop when the shell is exited.
nohupmakes the command ignoreNOHUPandSIGHUPsignals from the shell&makes the process run in the background in a subterminaldisownfollowed by an argument (the index of the job number in your jobs list) prevents the shell from sending aSIGHUPsignal to child processes. Usingdisownwithout an argument causes it to default to the most recent job.
I found thenohup anddisown information atthis page, and the& information inthis SO answer.
Update
When I originally wrote this, I was using it for data processing scripts/programs. For those kinds of use cases, something likets (task-spooler), works nicely.
Comments
I typically usescreen for keeping background jobs running.
1) Create a screen session:
screen -S myScreenName2) Launch your scripts,services,daemons or whatever
3) Exit (detach) screen-session with
screen -dorshortcutALT+A then d
After few hundreds of years - if you want to resume your session (reattach):
screen -r myScreenNameIf you want to know if there's a screen-session, its name and its status (attached or detached):
screen -lsThis solution works on all terminal interpreters like bash, zsh etc.See alsoman screen
3 Comments
screen -d -m your command will spawn a screen session foryour command, and a followingexit will exit the shell.Explore related questions
See similar questions with these tags.





