Linux Services
Sources:
What is a service?
A service is a process or application which is running in the background, either doing some predefined task or waiting for some event.
Daemon is the actual term for those long-running background processes. A service actually consists of one or more daemons.
What is systemd
Systemd is a system and service manager for Linux operating systems. The systemd
process has PID 1. It is the first process to run in our system; it then starts all the required processes, including services.
If you look at Unix/Linux history, you will find the first process which starts up, is also known as init process. This process used to start other processes by using the rc files from
/etc/rc.d
directory. In the modern Linux systems,systemd
has replaced the init system.
Units in systemd
systemd
manages various parts of a system with the abstraction units .
There are different kinds of units:
Unit Type | File Extension | Description |
---|---|---|
Service | .service |
Defines and manages a system service. |
Socket | .socket |
Describes a socket for inter-process communication. |
Device | .device |
Describes a device that has been recognized by udev. |
Mount | .mount |
Defines a mount point for a filesystem. |
Target | .target |
Groups units for easier management or dependencies. |
Path | .path |
Monitors a file or directory for changes. |
Timer | .timer |
Schedules and runs units based on time events. |
Swap | .swap |
Manages swap space on the system. |
Automount | .automount |
Configures an automount point for a filesystem. |
See here for detailed explanation.
.service units in systemd
.service
units are service units, which explains how to manage a particular service in the system. In our daily life, we generally only have to work with these unit files.
Here are commands using systemctl
to manage services
List all the service units:
1
systemctl --type=service
Start the service:
1
sudo systemctl start <service name>
Enable a service from starting at boot:
1
sudo systemctl enable <service name>
Disable a service from starting at boot:
1
sudo systemctl disable <service name>
journalctl
systemd
runs the systemd-journald.service
, which stores logs in the journal from the different services maintained by systemd
in binary format.
To read these logs,
1 | sudo journalctl |
You can add users into systemd-journal
group to avoid using sudo
.
Advanced usage:
Find the logs of a service:
1
sudo journalctl -u <service name>
List previous boots:
1
sudo journalctl --list-boots
Continuous stream of logs:
1
sudo journalctl -f -u <service name>
service
command
Other than systemd
, you may also see people manage services using the service
command. The latter is an older utility and is more of a compatibility wrapper that can be used to manage services in a generic way, regardless of the underlying init system (which could be SysVinit, Upstart, or systemd
).
How to create a unit file
A unit file is a plain text ini-format file that encodes information about a unit.
Below is a simple example of a systemd
service unit file that might be used to run a web application.
1 | [Unit] |
Breakdown of the sections:
[Unit]
Description
: A brief description of the service.After
: Specifies the order in which units are started. This service will start afternetwork.target
, meaning it will only start after the network is up.
[Service]
ExecStart
: The command that will be executed to start the service. In this case, it runs a Python web application.WorkingDirectory
: Sets the working directory for the service.Environment
: Sets environment variables for the service. Here, it's setting the Flask environment to production.User
andGroup
: The user and group under which the service will run. This is typically set to a non-privileged user likewww-data
for security reasons.Restart
: Configures when the service should be restarted.always
means it will be restarted if it crashes or is stopped.RestartSec
: Time to wait before restarting the service.
[Install]
WantedBy
: Specifies the target that this service should be included in.multi-user.target
is a common target for services that need to run on a typical system.
How to use an unit file
Suppose the unit file is for a service,
Create the unit file: Save the above content as
/etc/systemd/system/<service name>.service
.Reload systemd:
1
sudo systemctl daemon-reload
Start the service:
1
sudo systemctl start <service name>.service