title: "Configuring Startup Service with systemd in Ubuntu"
date: 2021-12-01T22:44:28+08:00
slug: ubuntu_systemd_service
type: posts
draft: false
categories: ["Linux","Using Software"]
tags: ["ubuntu"]#
systemd#
Systemd is a Linux system tool used to start and manage daemons, and it has become the standard configuration for most distributions.
Background#
Historically, Linux startup used the init process.
The following commands are used to start services.
$ sudo /etc/init.d/apache2 start
# or
$ service apache2 start
This method has two drawbacks.
First, it has a long startup time. The init process starts processes in a serial manner, meaning that the next process will only start after the previous one has finished starting.
Second, the startup scripts are complex. The init process only executes the startup script and does not handle other tasks. The scripts need to handle various situations, which often makes the scripts lengthy.
Overview of Systemd#
Systemd was created to address these issues. Its design goal is to provide a complete solution for system startup and management.
According to Linux conventions, the letter "d" is an abbreviation for daemon. The name "Systemd" signifies that it is meant to supervise the entire system.
With Systemd, there is no need to use init anymore. Systemd replaces initd and becomes the first process of the system (PID equals 1), with all other processes being its child processes.
$ systemctl --version
The above command is used to check the version of Systemd.
Using systemd to Execute Shell Scripts at Startup#
General Steps#
Create a script that you want to execute immediately at startup. In this example, the script is stored at /home/navxin/Example/startup.sh, and its contents are as follows:
#!/bin/bash
# Create a file named StartupTouch.txt in the same directory as the script during startup
touch /home/navxin/Example/startup.sh.txt
The script to be executed at startup needs to have executable permissions in order to be run by systemd. Use the following commands:#
chmod u+x /home/navxin/Example/startup.sh
chmod g+x /home/navxin/Example/startup.sh
Go to the directory where systemd places its services. In this directory, you can see a large number of service configuration files. Use the following command:#
# Go to the systemd service directory
cd /usr/lib/systemd/system
# View the file list
ls -al
Create a new .service file in this directory to configure the startup script. In this example, the file name is StartupExample.service, and the executed command and configuration content in the file are as follows:
Create the service configuration file#
sudo touch /usr/lib/systemd/system/StartupExample.service
The following is the content of the StartupExample.service configuration file:
[Unit]
Description=Startup Example
[Service]
ExecStart=/home/navxin/Example/startup.sh
[Install]
WantedBy=multi-user.target
Try running the newly created service manually using the following command:#
# Manually run StartupExample.service
sudo systemctl start StartupExample.service
# View the running log
systemctl status StartupExample.service
# Delete the file created during the service test
rm -f /home/navxin/Example/startup.sh.txt
# Set the service to enable status to run at startup
sudo systemctl enable StartupExample.service
# Reboot the machine
systemctl reboot
Note: Unit#
Systemd can manage all system resources. Different resources are collectively referred to as Units.
There are a total of 12 types of Units.
Service unit: system services
Target unit: a group of multiple Units
Device Unit: hardware devices
Mount Unit: mount points of file systems
Automount Unit: automatic mount points
Path Unit: files or paths
Scope Unit: external processes not started by Systemd
Slice Unit: process groups
Snapshot Unit: Systemd snapshots, which can be reverted to a certain snapshot
Socket Unit: inter-process communication sockets
Swap Unit: swap files
Timer Unit: timers
The systemctl list-units command can be used to view all Units in the current system.
# List running Units
$ systemctl list-units
# List all Units, including those without configuration files or failed to start
$ systemctl list-units --all
# List all inactive Units
$ systemctl list-units --all --state=inactive
# List all failed Units
$ systemctl list-units --failed
# List all running Units of type service
$ systemctl list-units --type=service
# List all running Units of type mount
$systemctl list-units --type=mount
# The command is used to list all configuration files.
$systemctl list-unit-files
$systemctl list-unit-files --type=mount
Modify the myadmin.service file and add the following statement:
After=network-online.target remote-fs.target nss-lookup.target navxin-kn1.mount
Wants=network-online.target
The complete file content is as follows (some content is referenced from nginx.service):
# /lib/systemd/system/myadmin.service
[Unit]
Description=Start myAdmin web server
Documentation=https://www.lyhuilin.com/
After=network-online.target remote-fs.target nss-lookup.target navxin-kn1.mount
Wants=network-online.target
[Service]
Environment="WELCOME=Baicai myAdmin Base Environment."
ExecStartPre=/bin/echo ${WELCOME}
ExecStart=/baicai/systemdStart/my_admin/my_admin -c /baicai/systemdStart/my_admin/conf/config.yaml
ExecStop=/bin/kill -s TERM ${MAINPID}
[Install]
WantedBy=multi-user.target