Processes
Each program that is running is a process and each process has a unique PID (process ID). A program can spawn child processes that are running beneath it. Commands you execute on a command line are an example. They will be a child process of the shell.
Overview of keys and commands
Command/key | Description |
---|---|
uptime | Show uptime and system load |
free | Show total memory usage |
ps | List processes |
top | View processes live. Press q to quit. |
htop | View processes live. Press q to quit. |
kill | Terminate a process |
xkill | After executing this the window you click on will be terminated |
Ctrl+C | Stop/cancel the current (foreground) process |
Ctrl+\ | Stop current (foreground) process more forcefully |
Ctrl+Z | Freeze current (foreground) process |
bg | Move process to background |
fg | Move process to foreground |
Foreground and background
If you start a program that will run for a long time, it will occupy the command line until it finishes. You won't get the prompt back before then. You can start such programs in the background by placing an ampersand (&) at the end:
my_long_program &
If you did not use the ampersand, it is still possible to freeze the program with Ctrl+Z and then making it a background process with bg.
If you want to let a process continue even after you close the shell where it was started, then use:
nohup my_long_program &
Showing processes
To get an overview of running processes you can use the ps command. It has many arguments to customize the output. This is a useful set of arguments to show all processes of all users and visually show the hierarchy (child processes):
ps auxf | less
For an overview that updates live you can use the classic top or the more advanced htop:
The uptime command tells you how long the computer has been running and an indication of the CPU load (for the past 1, 5 and 15 minutes). The number means how many processes would like to keep a processor core fully occupied. E.g. if 2 processes want 100% of a core, then the load is 2. If you have a processor with 4 cores, it can handle a load of 4. Example output:
uptime
21:59:23 up 8 days, 22:07, 1 user, load average: 0.10, 0.13, 0.13
Command free -h shows how much memory is used and free. Argument -h makes the output human readable (in MB or GB).
Stopping processes
If you just entered a command and the process is running in the foreground, you can stop it with Ctrl+C. That normally works. If it doesn't work, you can try Ctrl+\ instead. That sends a different signal to the program and often causes a more abrupt stop.
You can kill any running process with kill PID. You need to know the PID (process ID) from e.g. the ps command. Alternatively you can use killall program_name to kill all processes with that name. In a desktop environment you can kill the process of a window by executing xkill and then clicking the window.
The kill command can send different signals to the process. Most useful to know about is signal 9. The command is kill -9 PID. This should work if the normal kill command doesn't. The process will be stopped abruptly and not given the chance to clean things up nicely.