Skip to content

Persistent logins

If you have a script that needs to continue running on a login server after you log out, you have several options.

nohup

The nohup command means no hang up - don't kill the process when you log out.

$ nohup ./my-script.sh &

Appending & runs the script in background, allowing you to logout immediately or do other work. nohup will write output to file nohup.out - to specify a different output file:

$ nohup ./my-script.sh > different-output-file.log 2>&1 &

As nohup writes to a plain log file, you lose some benefits of a persistent terminal session such as coloured text or some types of buffered output. For a persistent terminal session use tmux or screen.

tmux

tmux is essentially a terminal emulator that persists when you logout.

  • create: tmux new -s my-session1

  • detach: Ctrl+b d

  • reattach: tmux a -t my-session1

  • list sessions: tmux ls

  • kill session:

    • from inside an attached session: Ctrl+b, then run : kill-session OR run exit
    • from outside: tmux kill-ses -t my-session1

screen

screen is similar to tmux and mostly differs in command syntax and appearance.

  • create: screen -S my-session1

  • detach: Ctrl+a d

  • reattach: screen -r my-session1

  • list sessions: screen -ls

  • kill session:

    • from inside an attached session: Ctrl+a, k OR run exit
    • from outside: screen -X -S my-session1 quit