Gokul NK

@gokulnk

Linux Systemctl

Monitoring services and starting them on boot

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 going gaga over systemd checkout arguments against it as well. But personally I find it easy to manage. Since I am not well versed in Linux it helps me make less blunders and miss out on doing some things because of lazyness and unfamiliarity. I use a command called systemctl. Check if it is 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 server
Loaded: 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 ago
Docs: 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 process
Jan 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 argument
Jan 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 are using a monitoring service like zabbix and need to check if a service is active you can use

# systemctl is-active nginx
active

Ready for Restart

Inspite 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. Making sure that everything works on a restart is hence very 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 times we need to make sure that all the services we use are in the start up 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 which 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 fool proof but it can give you quick glance of missing services.

Docker, Ansible et al

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

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

If you liked this article and would like to read similar articles, don’t forget to clap.

Click and drag to clap more than once. 50 is the limit.

You can read the others articles from the series.

If you are interested in cryptocurrencies checkout

More by Gokul NK

Topics of interest

More Related Stories