As we all know, it will always be needed to run an application as a service because sometimes we want our application to run in the background as a service daemon. So, in that case, we create Systemd service files or unit files to run the application.
Introduction to systemd and service
systemd is an init system and system manager that has widely become the new standard for Linux distributions. Due to its heavy adoption, familiarizing yourself with systemd is well worth the trouble, as it will make administering servers considerably easier. Learning about and utilizing the tools and daemons that comprise systemd will help you better appreciate the power, flexibility, and capabilities it provides, or at least help you to do your job with minimal hassle.
We will be discussing the
systemctl command, which is the central management tool for controlling the init system. We will cover how to manage services, check statuses, change system states, and work with the configuration files.
First, let’s start out by discussing the difference between a service and a process. In Linux, a service is just a daemon, which is a client/server application that runs in the background.
A service continuously listens for incoming requests and sends a response based on the request given but the process is simply an application or a script that runs in the foreground or the background.
Basic Synopsis for creating a systemd service file
[Unit] Description=<General description of your application> [Service] type=simple ExecStart=<path_to_script>/<app_script.sh> #To start the application ExecStop=/bin/<path_to_script>/<app_stop_script.sh> #To stop the application [Install] WantedBy=multi-user.target
This is a basic structure of a service file.
Save this file with .service extension.
Now, let’s create a service file to run nginx as a service
[Unit] Description=mynginx- server start [Service] type=forking PIDFile=/run/nginx.pid WorkingDirectory=/usr/sbin/ ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
Save this file in /etc/systemd/system
[Unit] carries generic information about units that are not dependent on the type of unit.
|Description||A human-readable name for the unit.|
[Service] which carries information about the service and the process it supervises
|type=forking||The parent process is expected to exit. The child continues to run as the main service process.|
|PIDFile=/run/nginx.pid||Takes a path referring to the PID file of the service.|
|WorkingDirectory=/usr/sbin/||Current working directory relative to the app.|
|ExecStart=/usr/sbin/nginx||Command or script with arguements.|
|ExecReload=/usr/sbin/nginx -s reload||Commands to execute to trigger a configuration reload in the service.|
|ExecStop=/bin/kill -s QUIT $MAINPID||Commands to execute to stop the service started via ExecStart|
|PrivateTmp=true||If true, all temporary files created by a service in /tmp/ and /var/tmp/ will be removed after the service is stopped.|
[Install] section carries installation information for the unit.
|WantedBy=multi-user.target||It specifies how a unit should be enabled. multi-user.target.wants will be created within /etc/systemd/system and a symbolic link to the current unit will be placed within.|
To run this service
sudo systemctl start my-nginx.service
To see Status of Service
sudo systemctl status my-nginx.service
The output will look like this:
● my-nginx-service.service - mynginx- server start
Loaded: loaded (/etc/systemd/system/my-nginx-service.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2021-09-28 12:17:21 IST; 7h ago
Main PID: 1166 (nginx)
Tasks: 0 (limit: 18772)
‣ 1166 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
- Manual page of unit and service files