Master systemd: From Basics to Real-World Service Management on Linux
This article introduces systemd, explains its features and unit file syntax, and provides step‑by‑step practical examples for managing Nginx, Tomcat, and custom Java JAR services with systemd on modern Linux distributions.
systemd Introduction
systemd is the main init system on modern Linux, replacing the traditional init because init runs sequentially and only executes startup scripts. Since CentOS 7, systemd is the default process manager.
All resources managed by systemd are called Units. systemd provides commands such as systemctl, hostnamectl, timedatectl, and localectl, which replace older tools like chkconfig and service.
systemd Features
Latest distributions (RedHat 7, CentOS 7, Ubuntu 15…) use systemd.
CentOS 7 supports parallel service start, significantly speeding up boot.
CentOS 7 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 previous issues, e.g., ensuring child processes are terminated.
systemd Syntax
systemctl [command] [unit]Common commands:
start: systemctl start nginx stop: systemctl stop nginx restart: systemctl restart nginx reload: systemctl reload nginx enable: systemctl enable nginx disable: systemctl disable nginx status:
systemctl status nginxsystemd Unit File Description
Each Unit requires a configuration file.
Files reside in /usr/lib/systemd/system/ and are linked to /etc/systemd/system/ when enabled.
Unit files usually have a .service suffix.
System units are placed under /usr/lib/systemd/system/; user units have a separate directory.
Configuration sections are enclosed in brackets and are case‑sensitive.
Practical Example 1 – Manage Nginx with systemd
Install build dependencies and compile Nginx from source:
yum -y install gcc gcc-c++ openssl-devel pcre-devel gd-devel iproute net-tools telnet wget curl
wget http://nginx.org/download/nginx-1.15.5.tar.gz
tar zxf nginx-1.15.5.tar.gz && cd nginx-1.15.5
./configure --prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module
make -j 4 && make installStart Nginx directly:
/usr/local/nginx/sbin/nginx # start
/usr/local/nginx/sbin/nginx -s reload # reload
/usr/local/nginx/sbin/nginx -s quit # stopCreate a systemd service file for Nginx:
[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 – Manage Tomcat with systemd
Download and install JDK and Tomcat, set environment variables, and verify Java installation.
wget 120.78.77.38/file/jdk-8u231-linux-x64.rpm
wget 120.78.77.38/file/apache-tomcat-9.0.27.tar.gz
rpm -ivh jdk-8u231-linux-x64.rpm
tar -xf apache-tomcat-9.0.27
mv apache-tomcat-9.0.27 /usr/local/tomcat
sh /usr/local/tomcat/bin/startup.sh # start
sh /usr/local/tomcat/bin/shutdown.sh # stopSystemd unit for Tomcat:
[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 – Manage a Java JAR Service with systemd
Example start command for a JAR application:
java -jar decode.jar -Dconfig=/usr/local/abc/application.propertiesSample control 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 ;;
esacCorresponding systemd unit (abc.service):
[Unit]
Description=uams server
Wants=network-online.target
After=network.target
[Service]
Type=forking
WorkingDirectory=/usr/local/abc/
ExecStart=/bin/bash uams.sh start
ExecStop=/bin/bash uams.sh stop
ExecReload=/bin/bash uams.sh restart
Restart=on-failure
[Install]
WantedBy=multi-user.targetOriginal link: https://blog.csdn.net/weixin_43546282
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.
