paint-brush
Linux Systemctlby@nkgokul
5,173 reads
5,173 reads

Linux Systemctl

by Gokul N KFebruary 6th, 2019
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

From wikipedia
featured image - Linux Systemctl
Gokul N K HackerNoon profile picture

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#Examplesudo systemctl start nginx

Stop an application

sudo systemctl stop application#Examplesudo systemctl stop nginx

Restart an application

sudo systemctl restart application#Examplesudo systemctl restart nginx

Reload an application

sudo systemctl reload application#Examplesudo systemctl reload nginx

Check status

sudo systemctl status application#Examplesudo 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.txtsystemctl list-unit-files | grep service | grep enabled | awk '{print $1;}' > enabled.txtdiff -y loaded.txt enabled.txt#If you want a quick glance of missing ones you can also usediff -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.


Understanding promises in JavaScript_I have had a kind of “love and hate” relationship with JavaScript. But nevertheless JavaScript was always intriguing…_hackernoon.com


Understanding async-await in Javascript — Gokul N K — Medium_Async and Await are extensions of promises. So if you are not clear about the basics of promises please get comfortable…_hackernoon.com


Should I use Promises or Async-Await_I recently read a medium post where the author claimed that using async-await is better than using promises. While this…_hackernoon.com

If you are interested in cryptocurrencies checkout


10 things to know/do before investing in cryptocurrencies_“Never Invest In Something You Don’t Understand”_hackernoon.com


Beginner’s Guide to “Investing in Cryptocurrencies”_It has been more than an year since I started investing in cryptocurrencies. Following Warren Buffet’s advice of “Never…_hackernoon.com


Why comparing cryptocurrency prices is wrong_Price is an important indicator. But it can also be misleading in many cases._blog.goodaudience.com