Operations 10 min read

Master systemd: From Basics to Real-World Service Management

This article introduces systemd as the modern Linux init replacement, explains its key features and unit file syntax, and provides three hands‑on examples showing how to manage nginx, Tomcat, and a custom JAR service with systemd commands and configuration files.

Efficient Ops
Efficient Ops
Efficient Ops
Master systemd: From Basics to Real-World Service Management

systemd Introduction

systemd is the primary system daemon manager on modern Linux distributions, replacing the traditional init system because init handles processes sequentially and only runs startup scripts, which can cause blocking and lacks service management capabilities. Since CentOS 7, systemd is the default.

systemd Features

Adopted by latest systems (RedHat7, CentOS7, Ubuntu15…)

CentOS7 supports parallel service start‑up, significantly improving boot speed.

CentOS7 shutdown only stops running services, unlike CentOS6 which stops all.

Service start/stop no longer uses scripts under /etc/init.d.

systemd resolves previous issues such as services not terminating child processes.

systemd Syntax

systemctl [command] [unit]
# command options
# start: start the unit, e.g., systemctl start nginx
# stop: stop the unit, e.g., systemctl stop nginx
# restart: restart the unit, e.g., systemctl restart nginx
# reload: reload the unit, e.g., systemctl reload nginx
# enable: enable unit at boot, e.g., systemctl enable nginx
# disable: disable unit at boot, e.g., systemctl disable nginx
# status: show unit status, e.g., systemctl status nginx

systemd Unit Files

Each unit requires a configuration file to tell systemd how to manage the service.

Unit files are stored in /usr/lib/systemd/system/; enabling a service creates a symlink in /etc/systemd/system.

Unit files typically have the .service suffix.

The directory contains both system and user sub‑directories; system services reside in /usr/lib/systemd/system.

Configuration sections are delimited by brackets and are case‑sensitive.

Practical Example 1 – Managing 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 install

Typical manual start commands:

/usr/local/nginx/sbin/nginx          # start
/usr/local/nginx/sbin/nginx -s reload # reload
/usr/local/nginx/sbin/nginx -s quit   # stop

Create a systemd service file:

[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.target

Control the service with systemctl:

systemctl restart nginx
systemctl enable nginx
systemctl stop nginx
systemctl status nginx

Practical Example 2 – Managing Tomcat with systemd

Install Java runtime:

wget 120.78.77.38/file/jdk-8u231-linux-x64.rpm
rpm -ivh jdk-8u231-linux-x64.rpm

Set environment variables in /etc/profile:

export JAVA_HOME=/usr/java/jdk1.8.0_231-amd64
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=${JAVA_HOME}/bin:$PATH
source /etc/profile
java -version

Install Tomcat:

tar -xf apache-tomcat-9.0.27.tar.gz
mv apache-tomcat-9.0.27 /usr/local/tomcat
sh /usr/local/tomcat/bin/startup.sh   # start
sh /usr/local/tomcat/bin/shutdown.sh  # stop

Create a systemd 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.target

Control Tomcat with systemctl:

systemctl restart tomcat
systemctl enable tomcat
systemctl stop tomcat
systemctl status tomcat

Practical Example 3 – Managing a custom JAR with systemd

Run a JAR directly:

java -jar decode.jar -Dconfig=/usr/local/abc/application.properties

Create a wrapper 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

Create a systemd unit for the JAR:

[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.target

Control the custom service:

systemctl restart abc
systemctl enable abc
systemctl stop abc
systemctl status abc
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSystem AdministrationService Managementinitsystemd
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.