Once you install the software( assume any node app that servers APIs) on servers(a VM instance or an EC2 instance) especially those that run in the background such as web servers or database servers. You would need to make sure that those servers or services are running and that they stay running even after the servers are rebooted.
So services in Linux help you configure the software to run in the background and make sure that they run all the time automatically when servers are rebooted as well as follow the right order of startup.
When any software that runs as a service in the background is installed such as a web server or database server or DevOps tools like docker it is automatically configured as a service on the system.
To start that service you run the service start command and provide the service name which happens to be in this case httpd
.
$ service httpd start
The newer method to start a service is to use the systemctl command and run the systemctl start command and provide the service name.
systemctl start httpd
System CTL is the command used to manage services on a systemd
managed server both the commands the service command and the system CTL command serve the same purpose. The service command uses the system CTL utility underneath so we will just focus on using the system CTL command for the remainder of this blog.
To stop a running service run the system CTL stop command followed by the service name.
$ systemctl stop httpd
To check the status of a service run the system CTL status command.
$ systemctl status httpd
To configure a service to start automatically when the system boots up run the system CTL to enable the command.
$ systemctl enable httpd
To disable the service at boot up run the system CTL disable command
$ systemctl disable httpd
Once services are configured it is so convenient for administrators to start, stop or enable the services. You don't have to go looking for the executables to start these services.
So how do you configure a program or software as a service?
Say for example you have a simple Python program and it could be a simple web server that's developed in Python and the code is available at /pythonApp/main.py
. You can run this server with the Python interpreter.
python3 /pythonApp/main.py
Once it's running if you head over to the localhost:5000 you see it return 'Hello, World !' which happens to be the response of the web server.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run()
Now I want to configure this as a service. You want your program to run as a service so that when you run the systemctl start flaskApp
command it starts and when you run the systemctl stop flaskApp
command it stops. Similarly, you want to configure your application to automatically start when the system boots up and even automatically restarts in case the application crashes.
How do you do that?
As we just saw the system called command line utility is used to manage the systemd services. So we must configure our program as a systemd service.
so how do we do that?
a systemd service is configured using a systemd unit file these files are located at /etc/systemd/system
.
So let's create a unit file at /etc/systemd/system.
The file must be named with the name that you eventually want the service to be known as in this case we'll name it FlaskApp
and with an extension dot service.
# flaskApp.service
[Service]
ExecStart= /usr/bin/python3 /code/pythonApp/main.py
Define a section called service using the square brackets inside the file and provide a directive named ExecStart
under it. This is where you specify the command that you will be using to run your application. So the same command that we run previously we're going to put it over here and that's it, that's enough to configure your application as a service.
run the
systemctl daemon-reload
command to let systemd know that there is a new service configured.And then run the
systemctl start flaskApp
command and provide the name of the service.let's check the status of the new service using the
systemctl status flaskApp
command it lists that the service is in an active and running state.you can now test by performing
curl https://localhost:5000
to stop the application run the
systemctl stop flaskApp
okay so we have configured our application to run as a service and we are now able to start and stop it as required.
So how do we configure it to automatically run when the system boots up?
We configure that in the unit file and the unit configuration file has many other options. The service section is only one of the many sections another section is the [Install]
section.
So in this section, we need to configure this service to run after a particular service that runs at boot up so one way to specify that is by using the WantedBy
directive.
We can configure this service to run after the multi-user target run level is started.
# flaskApp.service
[Service]
ExecStart= /usr/bin/python3 /code/pythonApp/main.py
[Install]
WantedBy=multi-user.target
once this is done you can configure the service to start during boot-up using the system cuddle enable command.
You can also provide additional metadata information about the service such as a description that would allow others to understand what this service is about. Now for that add a new section in the file called unit and use the description directive.
# flaskApp.service
[Unit]
Description=My python web application
[Service]
ExecStart= /usr/bin/python3 /code/pythonApp/main.py
[Install]
WantedBy=multi-user.target
If your application has other dependencies such as commands or scripts that are to be run before starting the application or after starting the application then add the ExecStartPre
and ExecStartPost
directives and specify the scripts or commands to be run.
# flaskApp.service
[Unit]
Description=My python web application
[Service]
ExecStart= /usr/bin/python3 /code/pythonApp/main.py
ExecStartPre= /code/pythonApp/requirements.sh
ExecStartPost= /code/pythonApp/email_status.sh
[Install]
WantedBy=multi-user.target
if you'd like the application to automatically restart in case it crashes specify the restart
directive and set its value to always.
# flaskApp.service
[Unit]
Description=My python web application
[Service]
ExecStart=/usr/bin/python3 /code/pythonApp/main.py
ExecStartPre=/code/pythonApp/requirements.sh
ExecStartPost=/code/pythonApp/email_status.sh
Restart=always
[Install]
WantedBy=multi-user.target
So let's take an example of real software say, docker. Well we're not going to talk about docker itself. We will have some next blogs coming on docker where we'll talk a lot about docker. In this context, docker is just an example of software so and it's the same with any software.
So you don't need to know anything about docker to understand this blog. so once docker is installed docker daemon runs as a background process that listens for docker commands. when docker is installed an executable named dockerd is made available on the system at the path /usr/bin/dockerd
.
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
[Service]
Type=notify
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
[Install]
WantedBy=multi-user.target
This is configured as a service using the system unit file at the path
/lib/systemd/system/docker.service
.
Now this file has three sections the [Unit]
service and [Install]
section as we just discussed and you can see the description as well as some of the other details such as a link to documentation, etc. The [Service]
section has the command that is run to start the docker daemon which is specified under the ExacStart
.
That's too much now for this one, wrapping this blog with the assumption that you at least got a picture of what services are and what power it holds!
See you in the next one and yeah we will create our own webserver using services on AWS...