Simple systemd on Linux

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
Search for a command to run...

I am a full stack developer mainly using golang. I work on Cloud at HPE, Bangalore, India.
No comments yet. Be the first to comment.
What is gRPC? gRPC is a modern open source high performance RPC framework that can run in any environment. It can efficiently connect services in and across data centers with pluggable support for load balancing, tracing, health checking and authenti...
Let's assume we have a sample Golang application (in a Go project folder). Contents of main.go package main import "fmt" func main() { fmt.Println("hello world") } In the same folder, run the following commands to initialize go modules # initial...

When you create a new Kubernetes Cluster in EKS - you still need to do several things until you can start using it. This simple set of steps aims to help people create an EKS cluster in simple steps and configure the most essential services like Log ...

To implement a stack in golang we can make use of the slice datatype which golang provides which allows us to modify the size on the fly. If we were doing it in C, we would need to define a max size for the array. Lets start with a simple structure ...

The purpose of this guide is to make a simple telegram bot, which replies with a compliment for each and every message that it receives. Create a bot in telegram Look for a bot named BotFather https://t.me/botfather . This bot is used to create bots...

Below is a super simple guide to creating a systemd service on linux. Systemd is not yet supported on all the linux systems, but it's popular and easy to use.
systemd is a Linux initialization system and service manager that includes features like on-demand starting of daemons, mount and automount point maintenance, snapshot support, and processes tracking using Linux control groups.
In simple terms, it helps us manage the lifecycle of services. You can add services to startup jobs, if your services die, it can auto restart and much more.
Requirements:
Install flask using pip3
sudo pip3 install flask
Make a file main.py, place it in any folder ex - /home/mridul/main.py and add the following contents in it.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
app.run(port=8000)
To run out flask server -
/usr/bin/python3 /home/mridul/main.py
To test if our application is running -
curl http://localhost:8000
You should be able to see Hello World! in the curl output.
To begin with the systemd service entry will be stored in /etc/systemd/system/
So, create a new file /etc/systemd/system/flaskapp.service
[Unit]
Description=Simple flask app service
After=network.target
StartLimitIntervalSec=0
[Service]
Type=simple
Restart=always
RestartSec=1
User=mridul
ExecStart=/usr/bin/python3 /home/mridul/main.py
[Install]
WantedBy=multi-user.target
Most of the above fields are easy to understand. The After field is used to start our service only after the network service runs. We can also put something else like mysql.target to run it after mysql service runs.
In the User field, we need to provice which user should the service be run from, for our example we can run it from my user i.e. mridul.
The ExecStart is where we give the command to run our service.
Restart is used to specify the behavior when our service dies due to some reason. setting it to always will restart our service everytime it dies, even if it exited with status code 0. To restart only in case of failure we can set it to on-failure.
To start our service
sudo systemctl start flaskapp
To stop our service
sudo systemctl stop flaskapp
To restart our service
sudo systemctl restart flaskapp
To check status of our service
sudo systemctl status flaskapp
To start our service during system startup
sudo systemctl enable flaskapp