How can I run a command which will survive terminal close?

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

  • Difference between nohup, disown and & (be sure to read the comments too)


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

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.