Linux processes
Processes are the programs that are running on your machine. They are managed by the kernel, where each process will have an ID associated with it, also known as its PID. The PID increments for the order in which the process starts. I.e. the 60th process will have a PID of 60.
View Running Processes in Linux#
We can use the friendly ps command to provide a list of the running processes as our user’s session and some additional information such as its status code, the session that is running it, how much usage time of the CPU it is using, and the name of the actual program or command that is being executed:
The ps command without any options displays information about processes that are bound by the controlling terminal.
To see the processes run by other users and those that don’t run from a session (i.e. system processes), we need to provide aux to the ps command: ps auxf
- The a flag stands for ‘all.’ When used with ps, it lists processes from all users on the system.
- The u flag stands for ‘user.’ It provides detailed information about each process, including the user that owns the process.
- The x flag stands for ‘extended.’ It lists processes not attached to a terminal, such as system services.
- The f flag do full-format listing
Another tool is top which can help you see all of the processes running on your system with live usage statistics.
Process management#
Inter-process communication and process termination are handled by sending specific kernel signals via the kill utility using the syntax:
kill -[SIGNAL] [PID]plaintextTo kill a process use kill [pid].
This sends the TERM signal to the process. The TERM signal tells the process to please terminate. This allows the program to perform clean-up operations and exit smoothly.
If the program is misbehaving and does not exit when given the TERM signal, you can escalate the signal by passing the KILL signal:
kill -KILL [pid]
This is a special signal that is not sent to the program. Instead, it is given to the operating system kernel, which shuts down the process. This is used to bypass programs that ignore the signals sent to them.
| Signal Name | Standard Constant | Operational Behavior |
|---|---|---|
| SIGTERM | 15 | Terminate Gracefully: Requests the process to stop. Allows the application time to save states, release network handles, clean up temporary files, and exit cleanly. |
| SIGKILL | 9 | Force Kill: Instantly terminates the process at the kernel level. The application cannot intercept, block, or handle this signal, preventing any post-execution cleanup. |
| SIGSTOP | 19 / 17 | Suspend/Pause: Freezes the current execution state of the process, placing it in the background without terminating its memory footprint. |
Namespaces - Process Isolation#
The kernel uses namespaces to partition physical hardware resources (such as CPU, memory, and execution priority) into isolated environments. This architectural slicing ensures security and operational stability: processes can generally only discover and interact with other processes residing within the same namespace boundary.
The process with an ID of 0 is a process that is started when the system boots. This process is the system’s init on Ubuntu, such as systemd, which is used to provide a way of managing a user’s processes and sits in between the operating system and the user.
For example, once a system boots and it initialises, systemd is one of the first processes that are started. Any program or piece of software that we want to start will start as what’s known as a child process of systemd. This means that it is controlled by systemd, but will run as its own process (although sharing the resources from systemd) to make it easier for us to identify and the likes.
Job Control: Foregrounding & Backgrounding#
Processes can run in two states, either an active foreground state or a non-blocking background state.
Executing a long-running process (e.g., file transfers or compilation scripts) in the foreground locks the shell interactive prompt. To mitigate this:
- Appending &: Appending an ampersand to a command (e.g., script.sh &) forks the process straight to the background, returning control of the terminal prompt immediately. Standard stdout/stderr outputs are suppressed or redirected.
- Ctrl + Z Suspension: Pressing Ctrl + Z during an active foreground process sends a SIGSTOP signal, immediately pausing execution and returning control to the prompt.
- If you wish to start running it in the background, then type
bgafter pressingctrl-z. - If you wish to have it run in the foreground, type
fgafter pressingctrl-z - To list all of the suspended processes in the background, we can use the
jobscommand.
You can additionally rundisownto detach the now-backgrounded process from the terminal. This lets you close the terminal window without affecting the backgrounded program.
Service Management via systemd#
Persistent background system processes (daemons) are administered via the systemctl utility. This tool communicates directly with systemd to manage service states and system boot properties.
systemctl [action] [service_name]plaintextCore Operational Controls#
- start: Immediately initialises and executes the service for the current session.
- stop: Halts a running service immediately.
- status: Inspects runtime diagnostics, PIDs, and error logging outputs for the specified daemon.
- enable: Configures the service to automatically initialize during the system boot sequence.
- disable: Prevments the service from auto-starting on system boot (manual start remains possible).
- start apache http web server
sudo service apache2 start
starts an apache2 server which will host the location /var/www/html - python http server
python3 -m http.server 80
starts a python server which will host everything in the current directory