How to use the systemctl command in Linux

Gokul N K
Gokul N K
Feb 7 · 3 min read
Photo by TBIT on Pixabay

Here’s the definition of Systemd from Wikipedia:

Systemd is a software suite that provides fundamental building blocks for a Linux operating system. It includes the systemd “System and Service Manager”, an init system used to bootstrap user space and manage user processes. systemd aims to unify service configuration and behavior across Linux distributions.

Before you go gaga over Systemd, check out the arguments against it as well. Personally, I find it easy to manage. Since I’m not well-versed in Linux, it helps me make fewer blunders, and it prevents me from missing out on doing some things because of laziness and unfamiliarity. I use a command called systemctl. Check if it’s supported in your Linux Distro or not.

# which systemctl
/bin/systemctl

Handy Commands

Start an application

sudo systemctl start application
#Example
sudo systemctl start nginx

Stop an application

sudo systemctl stop application
#Example
sudo systemctl stop nginx

Restart an application

sudo systemctl restart application
#Example
sudo systemctl restart nginx

Reload an application

sudo systemctl reload application
#Example
sudo systemctl reload nginx

Check status

sudo systemctl status application
#Example
sudo systemctl status nginx

Example output of status

 nginx.service - A high performance web server and a reverse proxy serverLoaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)Active: active (running) since Wed 2019-01-30 11:29:16 IST; 1 weeks 0 days agoDocs: man:nginx(8)Main PID: 909 (nginx)Tasks: 2 (limit: 1152)CGroup: /system.slice/nginx.service├─909 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;└─911 nginx: worker processJan 30 11:29:15 ubuntu-s-1vcpu-1gb-blr1-01-monitoring systemd[1]: Starting A high performance web server and a reverse proxy server...Jan 30 11:29:16 ubuntu-s-1vcpu-1gb-blr1-01-monitoring systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argumentJan 30 11:29:16 ubuntu-s-1vcpu-1gb-blr1-01-monitoring systemd[1]: Started A high performance web server and a reverse proxy server.

Check if active

If you’re using a monitoring service like Zabbix and need to check if a service is active, you can use:

# systemctl is-active nginxactive

Ready for Restart

In spite of all the precautions we take, there will be scenarios where things fail. As debugging might take more time and can put your application or service out of action, we might be forced to restart our server. Because of that, making sure that everything works on a restart is critical.

List all loaded units

systemctl list-units -all | grep loaded | awk '{print $1;}'

List all enabled units

systemctl list-unit-files| grep enabled | awk '{print $1;}' > enabled.txt

Most of the time, we need to make sure that all the services we use are in the startup script.

List all loaded services

systemctl list-units -all | grep service | grep loaded | awk '{print $1;}'

List all enabled services

systemctl list-unit-files | grep service | grep enabled | awk '{print $1;}' > enabled.txt

To find the list of services that are loaded but not enabled, we can do the following:

systemctl list-units -all | grep service | grep loaded | awk '{print $1;}' > loaded.txt
systemctl list-unit-files | grep service | grep enabled | awk '{print $1;}' > enabled.txt
diff -y loaded.txt enabled.txt
#If you want a quick glance of missing ones you can also use
diff -y loaded.txt enabled.txt | grep '<'

I know this is not foolproof, but it gives you a quick glance of missing services.


Docker, Ansible, Et Al.

Making sure that your server works as expected even after restarts lets you sleep easier at night.

We’re considering moving our workflow to Docker and Ansible. That will take some time. Until then, we’ll make do with a general setup, hacks like these, and a monitoring tool like Zabbix. Hope it helps you, too.

Better Programming

Advice for programmers.

Gokul N K

Written by

Gokul N K

Inquisitive, Student, Teacher and a wanna be Story teller. Working on https://learningpaths.io/

Better Programming

Advice for programmers.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade