Unix & Linux: How can I run a command which will survive terminal close? (11 Solutions!!)
Channel: Roel Van de Paar
Date: 2020-08-06

Unix & Linux: How can I run a command which will survive terminal close?


The Question: 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?

Solutions: Please watch the whole video to see all solutions, in order of how many people found them helpful

== This solution helped 350 people ==
One of the following 2 should work:
$ nohup redshift &
or
$ redshift &
$ disown
See the following for a bit more information on how this works:
 * man nohup
 * help disown
 * https://unix.stackexchange.com/questions/3886/difference-between-nohup-
 disown-and (be sure to read the comments too)

== This solution helped 149 people ==
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

== This solution helped 32 people ==
You can run the process like this in the terminal
setsid process
This will run the program in a new session. As explained http://hanoo.org/
index.php?article=run-program-in-new-session-linux

== This solution helped 52 people ==
Good answer is already posted by @StevenD, 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.

With thanks & praise to God, and with thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user w00t (https://unix.stackexchange.com/users/10381), user user308879 (https://unix.stackexchange.com/users/288843), user tony (https://unix.stackexchange.com/users/157058), user Steven D (https://unix.stackexchange.com/users/1207), user Stefan (https://unix.stackexchange.com/users/1327), user sondra.kinsey (https://unix.stackexchange.com/users/176171), user roaima (https://unix.stackexchange.com/users/100397), user Matthew (https://unix.stackexchange.com/users/115), user jozxyqk (https://unix.stackexchange.com/users/54030), user hanoo (https://unix.stackexchange.com/users/26549), user G-Man Says 'Reinstate Monica' (https://unix.stackexchange.com/users/80216), user gertvdijk (https://unix.stackexchange.com/users/23363), user daGo (https://unix.stackexchange.com/users/123973), user apolinsky (https://unix.stackexchange.com/users/11817), user Anthon (https://unix.stackexchange.com/users/33055), and the Stack Exchange Network (http://unix.stackexchange.com/questions/4004). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything is amiss at Roel D.OT VandePaar A.T gmail.com.