Mastering systemd: From Basics to Real-World Service Management
This article introduces systemd, explains its advantages over init, details unit concepts and key commands, outlines configuration file structures, and provides three hands‑on examples—managing Nginx, Tomcat, and a custom Java JAR service—demonstrating practical systemd usage on CentOS 7.
Introduction to systemd
systemd is the primary system daemon manager on modern Linux, replacing init because init handles processes serially and only runs startup scripts, leading to blocking and limited service management. Since CentOS 7, systemd is the default.
Features of systemd
All recent major distributions (RedHat 7, CentOS 7, Ubuntu 15, etc.) use systemd.
CentOS 7 supports parallel service start‑up, significantly improving boot speed.
Shutdown only stops running services, unlike CentOS 6 which stops everything.
Service start/stop no longer rely on scripts under /etc/init.d.
systemd resolves defects of the old model, such as services not terminating child processes.
systemd command syntax
systemctl [command] [unit]
# command options:
# start : systemctl start nginx
# stop : systemctl stop nginx
# restart : systemctl restart nginx
# reload : systemctl reload nginx
# enable : systemctl enable nginx (auto‑start on boot)
# disable : systemctl disable nginx (prevent auto‑start)
# status : systemctl status nginxConfiguration file overview
Each Unit requires a configuration file to tell systemd how to manage the service.
Files are stored in /usr/lib/systemd/system/ and linked from /etc/systemd/system/ after enabling.
Unit files use the .service suffix.
The directory contains separate system and user sub‑directories.
Configuration sections are case‑sensitive and enclosed in square brackets.
Practical Example 1 – Managing Nginx with systemd
Install build dependencies, compile Nginx, then create /usr/lib/systemd/system/nginx.service with appropriate sections and control it via systemctl commands.
# /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.targetPractical Example 2 – Managing Tomcat with systemd
Install JDK, extract Tomcat, then create /usr/lib/systemd/system/tomcat.service with environment variables and start/stop commands.
# /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"
Environment="CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
Restart=on-failure
[Install]
WantedBy=multi-user.targetPractical Example 3 – Managing a Java JAR service
Create a simple start/stop script ( demo.sh) and a systemd unit file ( /usr/lib/systemd/system/abc.service) that invokes the script.
# demo.sh
#!/bin/bash
source /etc/profile
jarName="abc-web.jar"
workDir="/usr/local/abc"
start() {
cd ${workDir} && java -jar ${jarName} --spring.profiles.active=prod --server.port=9630 >uams.log 2>&1 &
}
stop() {
ps -ef | grep -qP "(?<=-jar)\s+${jarName}" && kill $(ps -ef | grep -P "(?<=-jar)\s+${jarName}" | awk '{print $2}')
}
case $1 in
start) start ;;
stop) stop ;;
restart) stop; start ;;
esac # /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 demo.sh start
ExecStop=/bin/bash demo.sh stop
ExecReload=/bin/bash demo.sh restart
Restart=on-failure
[Install]
WantedBy=multi-user.targetControlling the services
Use systemctl restart, enable, stop, and status to manage the created services.
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.
Open Source Linux
Focused on sharing Linux/Unix content, covering fundamentals, system development, network programming, automation/operations, cloud computing, and related professional knowledge.
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.
