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

<code>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
</code>

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:

<code>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
</code>

Typical manual start commands:

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

Create a systemd service file:

<code>[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
</code>

Control the service with systemctl:

<code>systemctl restart nginx
systemctl enable nginx
systemctl stop nginx
systemctl status nginx
</code>

Practical Example 2 – Managing Tomcat with systemd

Install Java runtime:

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

Set environment variables in /etc/profile:

<code>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
</code>

Install Tomcat:

<code>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
</code>

Create a systemd unit for Tomcat:

<code>[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
</code>

Control Tomcat with systemctl:

<code>systemctl restart tomcat
systemctl enable tomcat
systemctl stop tomcat
systemctl status tomcat
</code>

Practical Example 3 – Managing a custom JAR with systemd

Run a JAR directly:

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

Create a wrapper script (demo.sh):

<code>#!/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
</code>

Create a systemd unit for the JAR:

<code>[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
</code>

Control the custom service:

<code>systemctl restart abc
systemctl enable abc
systemctl stop abc
systemctl status abc
</code>
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

login 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.