What are services?
When you start your computer, you might not realize that many things are already happening behind the scenes to make it ready for your use.
You might have software like Docker, MongoDB or Postgres, that automatically starts running in the background and when you need to work with those you don't need to start those services they are already running. So how is this magic happening? To understand that let's look at the definition of a service
A "service" refers to a program or process that runs in the background, often without the need for direct user interaction
Services are typically responsible for specific system tasks, such as managing network connections, handling file sharing, or providing web server functionality.
When do these services start?
Services are designed to start automatically when the computer boots up and continues running throughout the system's uptime. A few of the examples are the following.
Daemons
Web servers (e.g., Apache, Nginx)
Database servers (e.g., MySQL, PostgreSQL)
Web application servers (e.g., Tomcat, Node.js).
Let's talk about how we work with these services
To interact with the service we usually use two commands. Those commands are the followingsystemctl``service (SysV Init System)
Using service
(SysV Init System):
To enable a service to start automatically at boot time:
sudo service service-name start
To disable a service from starting automatically at boot time:
sudo service service-name stop
The actual commands may vary depending on your Linux distribution and init system (Systemd or SysV). The examples above demonstrate how to enable or disable a service. Make sure to replace service-name
with the actual name of the service you want to manage.
For instance, to enable the Apache web server to start at boot time with systemd, you'd use:
sudo systemctl enable apache2
And to disable it:
sudo systemctl disable apache2
Using systemctl
(Systemd Init System):
To start a service :
sudo systemctl start service-name
To stop a service:
sudo systemctl stop service-name
To check the status of a service about whether it is active or not:
sudo systemctl status service-name
To enable a service to start automatically at boot time:
sudo systemctl enable service-name
To disable a service from starting automatically at boot time:
sudo systemctl disable service-name
What does a simple service file look like?
Let's understand where these service files exist btw
Depending on your system they can exist somewhere like/etc/systemd/system/
//Run the following command to see all the systemd files
-> ls /etc/systemd/system
This path is dependent on the linux you are using.
Now let's understand the service file for simple docker service as an example
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
[Service]
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s SIGTERM $MAINPID
Restart=on-failure
User=root
Group=docker
[Install]
WantedBy=multi-user.target
Here's a brief explanation of a docker.service
unit file for systemd:
Name and Type: The name of the unit file is
docker.service
, and its type is set to "service," indicating that it manages a background service.Description: The description field provides a brief explanation of the service, which is typically "Docker Application Container Engine."
Service Section: This section contains configuration options related to how the Docker service should be managed. Some common options include:
ExecStart
: The command to start the Docker daemon.ExecReload
: The command to reload the configuration without stopping the service.ExecStop
: The command to stop the Docker service.Restart
: Specifies when the service should be automatically restarted, e.g., on failure.User
andGroup
: The user and group under which the Docker daemon runs.
Install Section: This section defines how the service should be installed and enabled at system boot:
WantedBy
: Specifies the target or runlevel at which this service should be enabled. It's often set tomulti-user.target
to start Docker when the system is in multi-user mode.
How can I write a make a custom service?
Since I am a full stack engineer, the first thing that comes to my mind is let's assume I have a node js app that I want to run as a service so that after boot time it starts automatically. You can create a file in the services folder of your system./etc/systemd/system/
- services folder
[Unit]
Description=My Node.js Server
Documentation=https://yourdocumentationurl.com
[Service]
ExecStart=/usr/bin/node /path/to/your/my-node-server.js
Restart=always
User=yourusername
Group=yourusergroup
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
I have saved the file as my-node-service.service
. Now the next steps would be
Reload systemd: Reload the systemd manager to recognize the new service:
shellCopy codesudo systemctl daemon-reload
Start and Enable the Service: Start your custom Node.js service and enable it to start at boot:
sudo systemctl start my-node-service
sudo systemctl enable my-node-service
Check the Service Status: You can check the status of your service:
sudo systemctl status my-node-service