• Sometimes you want to start a process and forget about it. If you start it from the command line, like this:
BASH CODE
redshift
[ad type=”banner”]
  • you can’t close the terminal, or it will kill the process. Can you run a command in such a way that you can close the terminal without killing the process?

  • close a terminal without killing the command running in it
  • One of the following 2 should work:
BASH CODE
$ nohup redshift &

OR

BASH CODE
$ redshift &
$ disown

  • 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:
BASH CODE
$ sleep 1000
^Z
[1]+ Stopped sleep 1000
$ bg
$ disown
$ exit

  • 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:
BASH CODE
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:
BASH CODE
nohup kate &
  • After closing konsole, pstree will look like this:
BASH CODE
init-+ 
|-kate---2*[{kate}
[ad type=”banner”]
  • and kate will survive.
  • An alternative is using screen/tmux/byobu, which will keep the shell running, independent of the terminal.

  • You can run the process like this in the terminal
BASH CODE
setsid process
  • This will run the program in a new session

  • use a variant on screen,the  command  like this
BASH CODE
screen bash -c 'long_running_command_here; echo; read -p "ALL DONE:"'
  • The session can be disconnected with Ctrl A Ctrl D and reconnected in the simple case with screen -r. you have this wrapped in a script called session that lives in my PATH ready for convenient access:
BASH CODE
#!/bin/bash
#
if screen -ls | awk '$1 ~ /^[1-9][0-9]*\.'"$1"'/' >/dev/null
then
echo "WARNING: session is already running (reattach with 'screen -r $1')" >&2
else
exec screen -S "$1" bash -c "$@; echo; echo '--------------------'; read -p 'ALL DONE (Enter to exit):'"
echo "ERROR: 'screen' is not installed on this system" >&2
fi
exit 1
  • This only works when you know in advance you want to disconnect a program. It does not provide for an already running program to be disconnected.

  • prefer:(applicationName &)

Example:

BASH CODE
           linux@linux-desktop:~$ (chromium-browser &)
[ad type=”banner”]
  • Make sure to use parenthesis when type the command!

  • You can set a process (PID) to not receive a HUP signal upon logging out and closing the terminal session.
  • Use the following command:
BASH CODE
nohup -p PID

Categorized in: