How to Manage Systemd Services and Units on ECS with the Systemctl Command

What Can You Manage with systemctl?

Example of the Anatomy of a systemd Unit File

# /lib/systemd/system/nginx.service
# Stop dance for nginx
# =======================
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
# nginx signals reference doc:
Description=A high performance web server and a reverse proxy server
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/n
systemctl --version

systemd Unit Types

ls /lib/systemd/system
man systemd.service
man systemd.timer

How to List Units Managed by systemd

systemctl list-units
systemd-journal-flush.service                                               loaded active exited    Flush Journal to Persistent Storage
ssh.service                                                                 loaded active running   OpenBSD Secure Shell server

How to Filter Units Listed by systemctl

systemctl list-units --all
systemctl --type timer list-units

How to Check Status of Units with systemctl

systemctl list-dependencies
systemctl status ssh.service
Loaded: masked (/dev/null; bad)
Process: 673 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
systemctl list-unit-files

How to Manage Units with systemctl

The commands in the next sections require superuser/root privileges. This means you either have to log in as the root user, or prefix the commands with “sudo”, if you’re logged in as a regular user (with sudo privileges). Example: if you’re not logged in as root, “systemctl disable nginx.service” should become “sudo systemctl disable nginx.service”.

systemctl disable nginx.service
systemctl enable nginx.service

Make Sure a Unit Can Never Start (Mask Unit)

systemctl mask nginx.service
systemctl unmask nginx.service
Failed to mask unit: File /etc/systemd/system/nginx.service already exists.
mv /etc/systemd/system/nginx.service /root

Manage Unit Running State with systemctl

systemctl start nginx.service
systemctl stop nginx.service
systemctl kill -s SIGKILL nameof.service
systemctl restart nginx.service
systemctl reload nginx.service

How to View and Edit Units with systemctl

systemctl cat ssh.service
systemctl edit --full ssh.service
systemctl revert ssh.service

How to Reboot, Shutdown, Standby and Hibernate with systemctl

systemctl reboot
systemctl poweroff
systemctl suspend
systemctl hibernate
systemctl hybrid-sleep


Original Source



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store