All processes in Linux are assigned a Process ID PID and can be viewed under the /proc/ directory along with other process information.

A process might also have a Parent Process ID PPID, which indicates that it was created as a child process to a parent process with the PPID.

systemd services

Most Linux distribution use the deamon systemd as their initialization system, meaning that is the first process that starts during boot process.

Manager: systemctl

The manager for systemd services is called systemctl.

CommandDescription
systemctl start <service>Start <service>
systemctl status <service>Show status of <service>
systemctl enable <service>Automatically start <service> on system boot
systemctl list-units --type=serviceList all services

Log query: journalctl

A tool to query systemd’s system logs (called the journal) is called journalctl

Process Status: ps

The Process Status command ps displays information about currently running processes.

A process can have the following states:

  • R: Running/Runnable
  • S: Sleeping (interruptible)
  • D: Uninterruptible sleep
  • T: Stopped/Traced
  • Z: Zombie (stopped but still has entry in process table)
  • I: Idle kernel thread
  • W: Paging/Swapping
  • X: Dead

Kill Process

Processes can be killed with:

  • kill: Sends signals to processes by PID
  • Combination of:
    • pgrep: Finds PIDs by name/pattern
    • pkill: Kills processes by name/pattern
  • killall: Terminates all processes with a given name

Kill signals

Processes can be killed in various ways and are done so by sending a specific kill signal.

All such signals can be viewed with kill -l.

The most common are:

NUMSIGNALDESCRIPTION
1SIGHUPSent when controlling terminal is closed
2SIGINTSent on [Ctrl]+C to interrupt process
3SIGQUITSent on [Ctrl]+D to quit
9SIGKILLImmediate termination, no cleanup
15SIGTERMProgram termination
19SIGSTOPStop program, cannot be handled
20SIGTSTPSent on [Ctrl]+Z to suspend, can be handled

Foreground/Background

Processes can run in the foreground meaning they run directly from your terminal and occupy it from being used to anything else. This means that it might receive input or send output on the terminal.

Processes running in the background means they are running withut being directly connected to the terminal, thus unable to receive input but allowing the terminal to be used for something else in the meanwhile.

The jobs command shows processes running in the background.

A service can be started and immediately sent to the background by appending & after the command.

A running service can be sent to the background by using CTRL + Z to suspend it and afterwards using the bg command to continues its execution in the background.

A process can be sent to the foreground by using the command fg <PID>.