Operations 10 min read

Mastering systemd: From Basics to Real-World Service Management on CentOS 7

This guide introduces systemd, explains its key features and unit file syntax, compares it with legacy init, and provides three hands‑on examples—setting up nginx, Tomcat, and a Java JAR—showing how to create, enable, and control services with systemctl on CentOS 7.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Mastering systemd: From Basics to Real-World Service Management on CentOS 7

1. Introduction to systemd

systemd is the primary system daemon manager on modern Linux distributions, replacing init because init handles processes serially and only runs startup scripts. Since CentOS 7, systemd is the default.

All resources managed by systemd are called Units. systemd provides commands such as systemctl, hostnamectl, timedatectl, localectl, which replace older commands like chkconfig and service.

2. Features of systemd

Adopted by the latest systems (RedHat 7, CentOS 7, Ubuntu 15…)

Parallel service start improves boot speed

Shutdown only stops running services, unlike CentOS 6 which stops all

Service start/stop no longer uses scripts under /etc/init.d Resolves issues such as services not terminating child processes

3. systemd command syntax

systemctl [command] [unit]
# command options
start   – start a unit, e.g., systemctl start nginx
stop    – stop a unit, e.g., systemctl stop nginx
restart – restart a unit, e.g., systemctl restart nginx
reload  – reload a 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

4. systemd unit file description

Each Unit requires a configuration file that tells 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 files use sections in square brackets and are case‑sensitive.

5. Relevant systemd files

File/Path

CentOS 6

CentOS 7

Service start script location

/etc/init.d

/usr/lib/systemd/system

Boot‑time enabled service directory

/etc/rcN.d

/etc/systemd/system/multi-user.target.wants/

Default runlevel configuration

/etc/inittab

/etc/systemd/system/default.target

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 -j 4 && make install

Start nginx manually:

/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

Practical Example 2: Install Tomcat and manage it with systemd

Install Java and Tomcat binaries:

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

Set environment variables in /etc/profile and source it.

Extract and move Tomcat:

tar -xf apache-tomcat-9.0.27
mv apache-tomcat-9.0.27 /usr/local/tomcat

Start and stop Tomcat manually:

sh /usr/local/tomcat/bin/startup.sh   # start
sh /usr/local/tomcat/bin/shutdown.sh  # stop

Create a systemd unit file 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

Manage Tomcat with systemctl:

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

Practical Example 3: Deploy a Java JAR with systemd

Write a simple start/stop script ( demo.sh) that runs the JAR and handles 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 ;;
esac

Create a systemd unit file for the JAR:

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

Control the 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.

LinuxService Managementsystemd
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.