459

Sometimes I want to start a process and forget about it. If I start it from the command line, like this:

redshift

I can't close the terminal, or it will kill the process. Can I run a command in such a way that I can close the terminal without killing the process?

Matthew
  • 5,537
  • 5
    Not a default install on all distros, but screen is your friend: http://en.wikipedia.org/wiki/GNU_Screen – Zayne S Halsall Nov 13 '10 at 14:23
  • 7
    To anyone facing the same problem: Remember, that even if you type yourExecutable & and the outputs keep coming on the screen and Ctrl+C does not seem to stop anything, just blindly type disown; and press Enter even if the screen is scrolling with outputs and you can't see what you're typing. The process will get disowned and you'll be able to close the terminal without the process dying. – Nav Apr 21 '16 at 11:18
  • 1
    You can use screen multiplexer such tmux. It is available via apt-get on ubuntu machines – Himanshu May 05 '16 at 11:17

11 Answers11

472

One of the following 2 should work:

$ nohup redshift &

or

$ redshift &
$ disown

See the following for a bit more information on how this works:

Stephen Kitt
  • 434,908
Steven D
  • 46,160
  • 9
    The second one (redshift & disown) worked for me on Ubuntu 10.10. It seems to work fine putting it all on one line. Is there any reason that I shouldn't do this? – Matthew Nov 13 '10 at 00:52
  • 1
    I'm not aware of any particular problem with that construction. – Steven D Nov 13 '10 at 02:09
  • 5
    @Matthew The first should work fine too, it just doesn't background like the second (you possibly want nohup redshift & so it does background). And putting the second on one line is fine, although usually you separate with ; (redshift &; disown) – Michael Mrozek Nov 13 '10 at 03:14
  • 13
    @Michael: Both ; and & are command separators and have equal precedence. The only difference is the synchronous versus asynchronous execution (respectively). There is no need to use &; in preference to just & (it works, but it is a bit redundant). – Chris Johnsen Nov 13 '10 at 04:29
  • 6
    good answer, one might add that it would be a good idea to redirect stdout and stderr so that the terminal won't be spammed with debug output – Kim Nov 13 '10 at 06:22
  • can we prepend nohup to any command that we might type in bash? If yes, how to make it prepended by default? – user13107 Oct 30 '12 at 06:46
  • 23
    Also, redshift &! will disown redshift immediately. –  Apr 21 '13 at 18:30
  • @stefan because it is easier to thing ahead. You do not want to have a list of stuff that needs disconnecting, when you close terminal. What if you forget? So do it ahead of time. Also for readshift, consider putting it in user start-up, so that it always runs. – ctrl-alt-delor Jul 21 '16 at 21:54
  • 1
    can't you provide a summary of the difference and save us time from reading the man page? I don't care about the details I just want it to work. – Charlie Parker May 01 '18 at 17:29
  • it does not work for tint2 :( – kokbira Nov 29 '19 at 11:30
  • For me only nohup python3 -m http.server 8000 & worked, whereas all other will be killed when closing the ssh connection. – User8461 Oct 22 '20 at 13:34
  • On macOS and zsh, using nohup command & resulted in the job being backgrounded, but when I went to exit the terminal session, it said zsh: you have running jobs. When I closed the terminal window, the job was no longer running in the background. However, command &! worked a treat! – Joshua Pinter Apr 03 '21 at 21:21
  • somecommand > /dev/null 2>&1 & disown somecommand ; exit (run a command in a background while redirecting all its outputs to nowhere(null), then disown it and close the terminal) – jave.web Jun 19 '23 at 23:16
176

If your program is already running you can pause it with Ctrl-Z, pull it into the background with bg and then disown it, like this:

$ sleep 1000
^Z
[1]+  Stopped                 sleep 1000
$ bg
$ disown
$ exit
Stefan
  • 25,300
  • 4
    after disown how can I again read the stdout of the running process? – Necktwi May 22 '14 at 10:59
  • 2
    @neckTwi, http://serverfault.com/questions/55880/moving-an-already-running-process-to-screen – Stefan May 22 '14 at 11:23
  • 1
    This didn't work for me for some reason (centos) – Ian Apr 17 '15 at 13:42
  • How to kill the process after this procedure? – Palak Darji May 02 '16 at 08:30
  • 2
    @necktwi disown does not disconnect stdout or stderr. However nohup does, as does >/dev/null (disconnect standard out), 2>/dev/null (disconnect standard error). It (disown) disconnects job-control. – ctrl-alt-delor Jul 21 '16 at 22:08
  • I couldn't get "nohup google-chrome &" working on Linux Mint (for whatever reason). This solution worked perfectly however, thanks! – BMB Aug 13 '17 at 14:04
  • 2
    To anyone else finding this, this really messed things up for me so be careful. Ctrl+z stopped it just fine, but bg made it start running again and now it won't respond to Ctrl+Z or Ctrl+C. Now there seems to be no way to exit the running command safely at all. No idea why, just did exactly what it said, but for whatever reason bg bought it back running. I can't do the next step of typing disown because there's too much output to type anything. – John Mellor Sep 20 '17 at 15:47
  • 1
    @robjbrain In bash jobs will tell you which jobs are running and their job number. Then use fg <job #> to bring a job to the foreground. – J.Money Sep 29 '17 at 12:59
  • 2
    @JohnMellor You were probably running into a similar situation to ctrl-alt-delor's comment, where the program is indeed running in the background but still has its stdout piped to your shell. Ctrl + C and Ctrl + Z won't do anything from here because no program is actually running in the foreground. You can still type in commands from here, like ls or disown without a problem, it is just really hard to read what you're typing while output is being mixed in with your characters. I just had a similar situation with ffmpeg, but it works fine. – RaisinBranCrunch Jul 22 '19 at 21:46
  • @palak Foregrounding it again with fg probably won't work, but you can note the PID of the process (in advance) as described in this post. You can also consider the comment by J.Money. – Cadoiz Oct 26 '21 at 12:29
60

Good answer is already posted by @Steven D, yet I think this might clarify it a bit more.

The reason that the process is killed on termination of the terminal is that the process you start is a child process of the terminal. Once you close the terminal, this will kill these child processes as well. You can see the process tree with pstree, for example when running kate & in Konsole:

init-+
     ├─konsole─┬─bash─┬─kate───2*[{kate}]
     │         │      └─pstree
     │         └─2*[{konsole}]

To make the kate process detached from konsole when you terminate konsole, use nohup with the command, like this:

nohup kate &

After closing konsole, pstree will look like this:

init-+
     |-kate---2*[{kate}]

and kate will survive. :)

An alternative is using screen/tmux/byobu, which will keep the shell running, independent of the terminal.

Cadoiz
  • 276
gertvdijk
  • 13,977
  • I had problems with the disown methods. This is currently working for me, so upvoting. I also like this because I can tail -f nohup.out to see whats happening, but not worry about my session failing – Ian Apr 17 '15 at 13:44
  • so after nohup or disown, how do you get the process back? or get access to old stdout? – fotoflo Mar 06 '21 at 13:00
  • Thank you very much for the recommendation screen/tmux/byobu. – spaceman Mar 17 '23 at 17:26
39

You can run the process like this in the terminal

setsid process

This will run the program in a new session, as explained in my article here.

AdminBee
  • 22,803
hanoo
  • 491
  • 4
  • 3
  • 1
    What do you see as the advantages of setsid over nohup? – itsbruce Oct 30 '12 at 10:52
  • 2
  • It doesn't print an annoying message about nohup.out. 2) It doesn't remain in your shell's job list, so it doesn't clutter the output of jobs.
  • – Mikel Nov 06 '12 at 22:24
  • 2
    This is the only solution that worked for with script that runs in lxterminal, launchs pcmanfm and exit (with setsid the terminal can close while pcmanfm keep running). Thank you so much! – desgua May 09 '19 at 14:48
  • It works for tint2, thanks – kokbira Nov 29 '19 at 11:32
  • Similar to desgua said with my Fedora GNOME install. Running a script to open other applications through my fish shell and a desktop application wrapper. nohup, open, xdg-open were not helping, even with the ampersand (&) and verifying in System Monitor in dependencies/hierarchy/family/tree mode that my new process was not a parent of the originally spawning-shell under gnome-terminal-server that was consistent with non- and actually- -working approaches. – Pysis Mar 15 '22 at 20:14