Start an application using systemd

systemd is used to start applications on linux systems.

In this post, we will create an application and run start it on boot using systemd.

Lets create our sample application.

mkdir /root/myapp/
vi /root/myapp/web.sh

Add following content to the file and save.

#!/bin/bash

echo "Starting web server" | systemd-cat -p info

cd /root/myapp
python3 -m http.server 80

You can start the application by running following command on terminal

bash /root/myapp/web.sh

this will run a simple web server on port 80. If you already have a web server running on port 80, change the port to another.

To stop web server, type CTRL+C.

Create Systemd service file

To manage this application using systemd, we need to create a service file.

vi /etc/systemd/system/myapp.service

Add

[Unit]
Description=Sample web server

[Service]
Type=simple
ExecStart=/bin/bash /root/myapp/web.sh

[Install]
WantedBy=multi-user.target

systemd service file

Managing Systemd service

First you need to enable the service with

systemctl enable myapp

To start the service, run

systemctl start myapp

To stop the service, run

systemctl stop myapp

To see status of the service, run

systemctl status myapp
Need help with Linux Server or WordPress? We can help!

Leave a Reply

Your email address will not be published. Required fields are marked *