157

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 . &$ exit

This 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. % exit

How do I make zsh's default behavior here like bash's?

Anko's user avatar
Anko
6,3765 gold badges37 silver badges48 bronze badges
askedOct 10, 2013 at 17:59
Owen_AR's user avatar
0

4 Answers4

263

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.

Bruno Duyé's user avatar
Bruno Duyé
1,14512 silver badges13 bronze badges
answeredNov 16, 2015 at 12:55
Anko's user avatar
Sign up to request clarification or add additional context in comments.

11 Comments

For completeness, can you also list how to disown it once it is already started?
@trusktrThere's a different question about disowning the foreground process. I think it's best kept separate for clarity. For anyone interested in zsh specifically, I've askeda question on U&L SE about how to create a faster workflow for disowning the foreground process.
I like this solution and I'll implement it for individual commands, but I'm still looking for a way to change Zsh's default behavior.
@Vince Do you mean changing the default to always disown backgrounded processes? If so, you can do that withsetopt 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.
@DimiDak Also works fine for me.alias z="sudo sleep 1 &|". Construct minimal examples like this, and you'll narrow it down eventually.
|
70

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 calledSIGHUP... 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_HUP
answeredOct 10, 2013 at 18:04
Carl Norum's user avatar

2 Comments

If you don't want the warning, yes.
Despite what the documentation says, this still kills the job for whatever reason. Tested on zsh 5.8: start an infinite loop-sleep script as a process, press ctrl+Z, run setopt, exit, and the process is killed.
53

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> & disown

While 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.

  • nohup makes the command ignoreNOHUP andSIGHUP signals from the shell
  • & makes the process run in the background in a subterminal
  • disown 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 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.

answeredMar 25, 2015 at 7:48
ryanjdillon's user avatar

Comments

8

I typically usescreen 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

orshortcutALT+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 alsoman screen

answeredMay 29, 2018 at 14:44
Aydin K.'s user avatar

3 Comments

A note for MacOS users, the keyboard shortcut to detach from screen was Control+A, then D. Also this was my only option when I left a process running and yielding to stdout, writing screen -d there wouldn't help.
Nice but nothing to do with the Q which is specifically about how to keep bg tasks running when exiting ZSH.
@RichieHH I don't believe you are correct. You can spawn "background tasks" and allow them to run when exiting ZSH:screen -d -m your command will spawn a screen session foryour command, and a followingexit will exit the shell.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.