1. Overview
In the realm of Linux administration, processes, and services play pivotal roles in ensuring a system’s smooth operation and functionality. While these two terms are often used interchangeably, there’s a subtle yet crucial distinction between them.
Understanding the differences between processes and services is essential for effectively managing a Linux system.
In this tutorial, we’ll theoretically understand the differences and importance of each. Furthermore, we’ll see some use cases and practical examples to understand when and how to use either option.
2. Theoretical Understanding
Before delving into practical illustrations, it’s important to understand theoretically the differences and capabilities of both a service and a process. Therefore, in this section, we’ll learn about services and processes.
2.1. Understanding Services in Linux
Linux services are vital background processes and daemons.
In essence, they’re mainly responsible for maintaining various system tasks such as:
- network services
- log management
- automated maintenance
Linux services’ role is to ensure the continuous functionality and stability of a Linux system while operating through background running daemons and automated tasks.
Next, let’s check the key features of a service in Linux:
- long-running: the design of services enables it to run continuously, providing persistent functionality
- auto-start: services are typically configured to start automatically when the system boots up
- sysctl management: by using the systemctl command, we can manage running services in our Linux environment
- daemonization: services often adopt a daemonized mode, detaching from the terminal and running independently
Notably, these are some of the key features of services that define their operation and functionalities.
In addition to understanding these key features, let’s check the most common Linux services:
- network services: provides network connectivity and communication services, such as DHCP, DNS, and web servers
- printing services: enables printing functionality, managing printers and print queues
- file system services: manages file system operations, including mounting and unmounting filesystems
- monitoring services: collects system performance data and provides monitoring tools
Accordingly, we conclude that services are typically initiated during system startup and continue running until the system shuts down. Moreover, services are often designed to respond to requests or events from users or other applications.
2.2. systemd Service Manager
systemd serves as the standard init system for service management in modern Linux distributions. Particularly, it acts as a service manager, introducing parallelization and dependency-based boot sequencing. Moreover, systemd replaces the traditional SysV init system, bringing uniformity to various processes, such as:
- starting services
- stopping services
- monitoring services
Services are often designed to respond to requests or events from users or other applications.
2.3. Understanding Processes in Linux
Processes in Linux represent instances of executing programs or scripts. Typically, they are dynamic entities, created and terminated as users interact with the system. In addition, understanding processes involves recognizing their dynamic nature, identifying them through Process IDs (PIDs), and managing them based on system resource requirements.
That being said, we might need to understand the dynamic nature of a process. Consequently, processes aren’t static entities; they’re created as users launch applications or execute commands. Accordingly, they consume system resources such as CPU and memory and may terminate upon completing their tasks. This dynamic nature necessitates effective process control mechanisms to ensure optimal system performance.
2.4. Process Identification and Characteristics
Every process is assigned a unique identifier, a process ID (PID), which allows the system to track and manage its execution. Furthermore, this numerical label distinguishes one process from another and serves as a means to manage and communicate with processes. In a nutshell, the PID is crucial for activities like terminating or adjusting the priority of a specific process.
Next, let’s see the characteristics that define a process:
- individuality: each process operates independently from other processes, with its own memory space and resource allocation
- state transition: processes can exist in various states, including running, waiting, sleeping, and zombie, where these states represent the different stages of a process’s life cycle
- communication: processes can communicate with each other through various mechanisms, such as pipes, sockets, and signals
- termination: when a process completes its execution, it terminates and releases its resources
By understanding the above key characteristics, we’re able to define a process from a service.
3. Practical Illustrations for Services
Service management involves various commands provided by systemd to control the state and behavior of services.
3.1. Managing Running Services
Let’s check how to manage services by the systemctl command:
$ sudo systemctl status apache2
● apache2.service - The Apache HTTP Server
Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2024-03-12 15:30:45 UTC; 30min ago
Main PID: 1234 (apache2)
Tasks: 55 (limit: 4915)
Memory: 78.2M
CGroup: /system.slice/apache2.service
├─1234 /usr/sbin/apache2 -k start
├─5678 /usr/sbin/apache2 -k start
└─9012 /usr/sbin/apache2 -k start
In the above example, at first, we used the sudo command to grant us superuser privileges. As an alternative, we can log in as a root user.
Furthermore, we used the systemctl status command followed by the service name, which is apache2. As shown by the output, the command provides a detailed overview of the current state of a specified service. Moreover, the output includes information on whether the service is active, inactive, or experiencing any issues.
Here, we check the status of the Apache web server (apache2). Accordingly, the output reveals significant information such as the service’s current state, its associated process ID (PID), memory usage, and recent log entries.
In addition, we can manage services by the start, stop or restart commands. Let’s check the syntax of each beginning by the start command:
$ sudo systemctl start nginx
The systemctl start command initiates the specified service, ensuring that it’s actively running. By executing this command, we start the Nginx web server. Hence, it’s particularly useful when we need to manually start a service without rebooting the system.
Next, we’ll move forward to the stop command. In essence, the systemctl stop command halts the specified service, terminating its associated processes:
$ sudo systemctl stop mysql
By bringing an orderly conclusion to its running processes, this command stops the MySQL database service (mysql).
Shifting towards the systemctl restart command, it gracefully restarts the specified service, terminating existing processes and initiating a fresh start:
$ sudo systemctl restart ssh
Here, we restart the SSH service, ensuring that any recent configuration changes take effect without the need for a complete service stop and start.
3.2. Managing Next Boot Services
In some situations, we might need to ensure a certain service is run once booting the system. On the other hand, we might want to stop a certain service from starting when we boot the system. This feature enables us to control systemd lookups for autostart files.
Let’s check this feature by utilizing the enable command:
$ sudo systemctl enable apache2
The enable command configures the specified service to start automatically during system boot. For instance, apache2 now automatically initiates during the boot process, ensuring the seamless availability of the web server.
This creates a symbolic link from the system’s copy of the service file into the disk location that systemd searches.
On the contrary, let’s check how to disable a service from running when the system first boots up:
$ sudo systemctl disable Nginx
By executing the disable command, Nginx is disabled, and it won’t automatically start upon system boot.
4. Practical Illustrations for Processes
Process control involves various commands for monitoring, terminating, and adjusting the priority of running processes.
For example, the ps aux command provides a comprehensive list of all running processes on the system. Additionally, this includes information such as the process ID (PID), CPU usage, memory utilization, and the command associated with each process:
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 12345 6789 ? Ss Feb01 0:01 /usr/lib/systemd/systemd
...
john 1234 2.0 3.4 123456 78901 pts/0 Sl+ Feb01 2:34 /usr/bin/example_process
...
The output of this command gives an overview of all active processes, making it an invaluable tool for monitoring system activity.
Next, let’s check out the kill command capabilities to manage a certain process:
$ kill 1234
$ ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 12345 6789 ? Ss Feb01 0:01 /usr/lib/systemd/systemd
...
As indicated from the output above, the kill command, followed by the process ID, enables us to terminate a specific process, in our case the process with PID 1234.
Particularly, we use this command when we need to terminate a specific process immediately.
In certain examples, the user might be aware of the process name and lack the knowledge of getting its PID. In other scenarios, to enable faster termination instead of searching for the PID of a certain process using the grep command. For example, a user might terminate a process directly by using its name:
$ pkill firefox
The pkill command not only enables the user to kill a process by its name, but it also ensures all instances of the Firefox browser will be terminated. This provides us a convenient way to close multiple processes associated with a particular application.
Similar to pkill, the killall command terminates all processes with the specified name:
$ killall chrome
This enables us to have control over all instances of the Chrome browser and terminate them. Finally, a great advantage in favor of the killall command is streamlining the process termination procedure.
5. Conclusion
In this article, we learned about services and processes using some practical use cases. Mastering the art of service management and process control is fundamental for effective Linux system administration.
The theoretical insights into the roles of services and the dynamic nature of processes provide a foundational understanding. Also, the practical illustrations of commands such as systemctl for service management and ps and kill for process control equip us with the tools needed to navigate and optimize our systems.