Operations 10 min read

Master systemd: Write unit files and control Nginx, Tomcat, and Java services

This guide explains what systemd is, its key advantages over traditional init, the basic systemctl syntax, how unit files are structured, and provides step‑by‑step examples for creating systemd services to manage Nginx, Tomcat, and a custom Java JAR application on CentOS 7 and similar Linux distributions.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master systemd: Write unit files and control Nginx, Tomcat, and Java services

What is systemd?

systemd is the default system and service manager on modern Linux distributions (e.g., CentOS 7, Red Hat 7, Ubuntu 15+). It replaces the older init system, which starts services sequentially and offers limited control, by providing parallel startup, better dependency handling, and a unified command set.

Key features of systemd

Adopted by all recent major Linux distributions.

Parallel service start‑up on boot, dramatically reducing boot time.

Shutdown only stops running services, avoiding unnecessary restarts.

Service scripts under /etc/init.d are no longer needed.

Improved handling of child processes and automatic cleanup.

systemd command syntax

systemctl [command] [unit]
# command options
#   start   – start the specified unit
#   stop    – stop the specified unit
#   restart – restart the specified unit
#   reload  – reload the specified unit
#   enable  – enable unit at boot (requires proper unit file)
#   disable – disable unit at boot
#   status  – show current status of the unit

Configuration file layout

Each service is defined by a unit file, typically with a .service suffix.

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

Files are divided into sections: [Unit], [Service], and [Install], using case‑sensitive keys.

Practical example 1 – Managing Nginx with systemd

Compile and install 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 -j4 && 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 unit file /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.target

Control the service with systemctl:

systemctl start nginx
systemctl enable nginx   # enable at boot
systemctl status nginx
systemctl stop nginx

Practical example 2 – Managing Tomcat with systemd

Install Java and Tomcat (binary tarball) and set environment variables:

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  # stop

Unit file /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.target

Manage Tomcat with systemctl:

systemctl start tomcat
systemctl enable tomcat
systemctl status tomcat
systemctl stop tomcat

Practical example 3 – Managing a custom Java JAR with systemd

Sample start/stop 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

Corresponding unit file /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 /path/to/demo.sh start
ExecStop=/bin/bash /path/to/demo.sh stop
ExecReload=/bin/bash /path/to/demo.sh restart
Restart=on-failure

[Install]
WantedBy=multi-user.target

Control the custom service:

systemctl restart abc   # start/restart
systemctl enable abc    # enable at boot
systemctl stop abc      # stop
systemctl status abc    # view status

These examples demonstrate how to create unit files, write supporting scripts, and use systemctl to manage services reliably across reboots.

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.

JavaLinuxNginxTomcatService Managementsystemdsystemctl
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.