Last modified by Yunhao Wu on 2023/07/05 16:11

How to run programs in a way, that they don’t terminate when you log out

Here is a description how you can still run programs after you have logged out. This happens by starting the programs on the server which is configured identically on the computers in the hall.

1. General information

If a student wants to run a computer intensive program at night, this is in principle not a problem, as far as the following points are taken into consideration:

 in order to connect to lxhalle via SSH you have to enter in the terminal the passphrase, that is standing below your cit.tum-Login (CIT/ITO-Login). Here is !HowTo for Windows users, who should set up !PuTTY, which enables the ssh connection via terminal. ssh

  • in order the programs to work without harming other computer users, they should be started with one of the lowest possible scheduling priorities. This happens with the command \“nice -n19\” as shown in the HowTo example. 

2. Script example

In order to demonstrate the functionality of the presented methods, below there is a small Shell-Script, that writes every single second the actual date in datum.txt. This is just an example of a program, that can run in the console. The script is composed by the following line of code:

%CODE{ lang=\"bash\" }% #!/bin/bash while true do sleep 1 date done >> datum.txt


3. nohup

In case a program is started with the nohup command, the HUP-signal, that is send by the logout is ignored. So the program proceeds further even after the user has already logged out

  • the following input nice -n 19 nohup ./ & starts the test script and the control operator (&) makes the command run in the background.
  • from now on the date is written in the file datum.txt, even after the user logs out
  • after running nohup the PID of the process is shown. PID can be used to close a program (if it doesn’t terminate automatically), to do so you should enter the following command kill PID.
  • now you can continue your work in the terminal or you can close it without terminating the program
  • by logging out and then once again in, you can check, whether in the file datum.txt it’s still written

4. screen

The screen command enables you to create more than one screen sessions, which once again can consist of up to 10 virtual consoles. In this consoles you can start programs, that remain even after the session of the terminal window is closed.

 First here are some basic control commands:

  • start screen:
    • nice -n 19 screen
    • command for starting a session with customized name (in our case named sitzung1) nice -n 19 screen -S sitzung1
  • at the beginning some information for the program is shown and then you can click the space bar, the console will still remain unchanged – you have started the screen session and you are now in the first virtual console
  • start one more virtual console:
    • Strg + A and then C
  • close a virtual console:
    • Strg + A and then K
  • switch between the virtual consoles:
    • Strg + A and then Leertaste
    • Strg + A and then number between 0 and 9 (for the corresponding console)
  • detach the current session:
    • Strg + A and then D
    • for a certain session (in this case sitzung1) screen -d sitzung1
  • reestablish connection to a session (here sitzung1)
    • screen -r sitzung1
  • end session
    • Strg + D
    • exit
    • close the last virtual console
  • list all running session with their names (example in picture be 6561) screen -ls
  • show all keyboard shortcuts
    • enter Strg + A and then ?
  • refer to the man pages for the full documentation of the screen command man screen

 Steps for using the screen (see the commands above):

  • start programs
    • open terminal window
    • start screen – thereby automatically creating a session, that is attached
    • run program
    • start new consoles and programs if needed
    • detach the current sessions
    • close terminal window
  • end programs
    • open terminal window
    • list all running screen sessions
    • resume desired screen sessions
    • end the programs in the virtual consoles (if they don’t terminate on their own)
    • close the virtual console respectively close the session
    • close terminal window