Mastering systemd: From Basics to Managing Nginx, Tomcat, and Java Services
This guide introduces systemd, explains its advantages over init, details unit concepts, command syntax, configuration files, and demonstrates practical setups for managing Nginx, Tomcat, and custom Java JAR services on CentOS 7, including installation, service files, and control commands.
1. Introduction to systemd
systemd is the primary system and service manager on modern Linux distributions, replacing the traditional init system because init handles processes serially and only runs startup scripts, leading to blocking and limited service control. systemd manages resources called Units and provides commands such as systemctl, hostnamectl, timedatectl, localctl.
2. Features of systemd
Adopted by the latest distributions (RedHat 7, CentOS 7, Ubuntu 15, …).
Supports parallel service start on boot, significantly improving boot speed.
Shuts down only running services on power‑off, unlike CentOS 6 which stops all.
Service start/stop no longer relies on scripts under /etc/init.d.
Fixes defects of the old model, e.g., proper termination of child processes.
3. systemd command syntax
systemctl [command] [unit]
# command options
# start – start a unit
# stop – stop a unit
# restart – restart a unit
# reload – reload a unit
# enable – enable unit at boot
# disable – disable unit at boot
# status – show unit status4. systemd unit file structure
Each Unit requires a configuration file that tells systemd how to manage the service.
Configuration files are stored in /usr/lib/systemd/system/; enabling a service creates a symlink in /etc/systemd/system/.
Unit files use the .service suffix.
Two directories exist: system (for services that start without a logged‑in user) and user.
Files are divided into sections using square brackets and are case‑sensitive.
5. Comparison of systemd‑related paths
Service start script path: /etc/init.d (CentOS 6) → /usr/lib/systemd/system (CentOS 7)
Boot‑auto start directory: /etc/rcN.d (CentOS 6) → /etc/systemd/system/multi-user.target.wants/ (CentOS 7)
Default run‑level configuration file: /etc/inittab (CentOS 6) → /etc/systemd/system/default.target (CentOS 7)
Practical Example 1 – Compile and install Nginx with systemd control
Install build dependencies:
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 -j4 && make installStart Nginx manually:
/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:
vim /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:
systemctl restart nginx
systemctl enable nginx
systemctl stop nginx
systemctl status nginxPractical Example 2 – Install Tomcat and manage it with systemd
Install Java runtime (RPM) and download Tomcat archive:
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.rpmExtract and move Tomcat:
tar -xf apache-tomcat-9.0.27.tar.gz
mv apache-tomcat-9.0.27 /usr/local/tomcatStart and stop Tomcat manually:
/usr/local/tomcat/bin/startup.sh # start
/usr/local/tomcat/bin/shutdown.sh # stopCreate a systemd service file for Tomcat:
vim /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.targetControl Tomcat with systemctl:
systemctl restart tomcat
systemctl enable tomcat
systemctl stop tomcat
systemctl status tomcatPractical Example 3 – Manage a custom Java JAR with systemd
Typical launch command:
java -jar decode.jar -Dconfig=/usr/local/abc/application.propertiesCreate a wrapper script (demo.sh) that supports start, stop, restart:
#!/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 file:
vim /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.targetControl the custom service:
systemctl restart abc
systemctl enable abc
systemctl stop abc
systemctl status abcSigned-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.
