Master systemd: From Basics to Real-World Service Management on Linux
Learn how systemd replaces traditional init on modern Linux, its key features, command syntax, unit file structure, and step-by-step hands-on examples that show how to create and control systemd services for Nginx, Tomcat, and custom Java JAR applications using systemctl.
systemd Overview
systemd is the default init system on modern Linux distributions such as CentOS 7, RedHat 7, and Ubuntu 15+. It replaces the traditional init, providing parallel service startup, better shutdown handling, and a unified command set (systemctl, hostnamectl, timedatectl, localectl).
Key Features
Adopted by the latest Linux distributions.
Parallel service start improves boot speed.
Only running services are stopped on shutdown.
No more /etc/init.d scripts; services are defined as Units.
Handles shortcomings of legacy service management (e.g., orphaned child processes).
systemd Command Syntax
systemctl [command] [unit]
# commands:
# start – start a unit, e.g., systemctl start nginx
# stop – stop a unit, e.g., systemctl stop nginx
# restart – restart a unit, e.g., systemctl restart nginx
# reload – reload configuration, e.g., systemctl reload nginx
# enable – enable unit at boot, e.g., systemctl enable nginx
# disable – disable unit at boot
# status – show unit status, e.g., systemctl status nginxUnit Configuration Files
Each Unit has a .service file placed in /usr/lib/systemd/system/. After enabling, a symlink is created in /etc/systemd/system/. The file consists of sections [Unit], [Service], and [Install], and is case‑sensitive.
Practical Example 1 – Managing Nginx with systemd
Compile and install Nginx from source, then create /usr/lib/systemd/system/nginx.service:
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.targetControl the service with:
systemctl start nginx
systemctl enable nginx
systemctl restart nginx
systemctl status nginxPractical Example 2 – Managing Tomcat with systemd
Install JDK, set environment variables, extract Tomcat, then create /usr/lib/systemd/system/tomcat.service:
[Unit]
Description=tomcat server
Wants=network-online.target
After=network.target
[Service]
Type=forking
Environment="JAVA_HOME=/usr/java/jdk1.8.0_231-amd64"
Environment="PATH=$JAVA_HOME/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin"
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.targetManage Tomcat with the usual systemctl commands (start, stop, enable, status).
Practical Example 3 – Managing a Java JAR with systemd
Write a simple Bash wrapper demo.sh that starts and stops the JAR, then create a service file /usr/lib/systemd/system/abc.service:
[Unit]
Description=uams server
Wants=network-online.target
After=network.target
[Service]
Type=forking
WorkingDirectory=/usr/local/abc/
ExecStart=/bin/bash /usr/local/abc/demo.sh start
ExecStop=/bin/bash /usr/local/abc/demo.sh stop
ExecReload=/bin/bash /usr/local/abc/demo.sh restart
Restart=on-failure
[Install]
WantedBy=multi-user.targetControl the JAR service with systemctl restart abc, systemctl enable abc, systemctl stop abc, and systemctl status abc.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
