Creating and managing services in Ubuntu Server 18 is essential for system administrators. In this article, we will explore how to create custom services using systemd. We will explain each option in detail and provide examples.
Essential Steps to Create a Service
Let’s start by creating a simple script. Suppose we have a script that logs the current time to a file.
- Create a file (sudo nano /usr/local/bin/time_logger) and put the code below and save it;
- Make a file executable (sudo chmod +x /usr/local/bin/time_logger).
#!/bin/bash
while true
do
echo "Current time: $(date)" >> /var/log/time.log
sleep 60
done
Create the Service File
Create a file. (sudo nano /etc/systemd/system/time_logger.service) and add the code below.
[Unit]
Description=Time Logger Service
After=network.target
[Service]
ExecStart=/usr/local/bin/time_logger
Restart=always
User=pac-man
Group=adm
[Install]
WantedBy=multi-user.target
Let’s look at the main options in this file:
[Unit]
Description: A description of the service.
After: Specifies that the service should start after the specified target. Here, it is network.target.
[Service]
ExecStart: The command that will be executed to start the service. In our case, it is the path to the script.
Restart: The policy for restarting the service. The always option means the service will be resumed on any exit.
User and Group: The user and group under which the service will run.
[Install]
WantedBy: Specifies the target to which the service should be attached. multi-user.target means the service will run in multi-user mode (standard mode for servers).
Starting and Managing the Service
After creating the service file, reload the service files and start the new service:
sudo systemctl daemon-reload
sudo systemctl start time_logger.service
To check the status of the service:
sudo systemctl status time_logger.service
To enable the service to start on boot:
sudo systemctl enable time_logger.service
To stop and disable the service:
sudo systemctl stop time_logger.service
sudo systemctl disable time_logger.service
Create a file, change the permissions, and change the owner
sudo touch /var/log/time.log
sudo chmod 660 /var/log/time.log
sudo chown pac-man:adm /var/log/time.log
# Example output
pac-man@lab-vm:/var/log$ sudo touch /var/log/time.log
pac-man@lab-vm:/var/log$ sudo chmod 660 /var/log/time.log
pac-man@lab-vm:/var/log$ sudo chown pac-man:adm /var/log/time.log
pac-man@lab-vm:/var/log$ sudo service time_logger start
pac-man@lab-vm:/var/log$ ll |grep time.log
-rw-rw---- 1 pac-man adm 138 Jul 12 07:04 time.log
pac-man@lab-vm:/var/log$
pac-man@lab-vm:/var/log$ cat time.log
Current time: Fri 12 Jul 2024 07:02:23 AM UTC
Current time: Fri 12 Jul 2024 07:03:23 AM UTC
Current time: Fri 12 Jul 2024 07:04:23 AM UTC
Current time: Fri 12 Jul 2024 07:05:23 AM UTC
pac-man@lab-vm:/var/log$
Troubleshooting
If there are issues with the service, you can use the journalctl command to view logs:
sudo journalctl -u time_logger.service
Example of a Full-Service
As an example, let’s take the nginx web server and create a custom service for it (even though it already has a built-in service):
# Install NGINX
sudo apt-get update
sudo apt-get install nginx
Create the Service File
Create the file /etc/systemd/system/custom_nginx.service and put the code below
[Unit]
Description=Custom Nginx Service
After=network.target
[Service]
ExecStart=/usr/sbin/nginx -g 'daemon off;'
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PIDFile=/run/nginx.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
Start and Manage
sudo systemctl daemon-reload
sudo systemctl start custom_nginx.service
sudo systemctl enable custom_nginx.service