Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit zsh, but leave running jobs open?

Tags:

bash

zsh

jobs

People also ask

What happens to paused or background jobs when you log out bash?

When a user logs out from bash all the background jobs that is started by the user will automatically terminate, if he is not using nohup or disown.

How do I exit zsh in terminal?

Pressing Enter on an empty line: execute: _ zsh: do you wish to see all 373 possibilities (64 lines)? In other shells, Alt + X brings the same behavior, except that Ctrl + C cancels the prompt.

Can't logout there are stopped jobs?

Typically this happens when a program is run and suspended (with ctrl z ). To get a list of such jobs, use the command jobs on the terminal and use fg to revive each of those jobs and quit them properly. Once this is done, terminal can be exit ed normally.

How do you use zsh shells?

On Ubuntu-based distros, you can install zsh using: sudo apt-get install zsh . Once the installation completes, you can check the version using zsh --version , then make zsh your default shell using chsh -s $(which zsh) . You'll need to log out, then log back in for the changes to take effect.


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.


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

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 ignore NOHUP and SIGHUP 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 a SIGHUP signal to child processes. Using disown 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.


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