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.
1 2 |
mkdir /root/myapp/ vi /root/myapp/web.sh |
Add following content to the file and save.
1 2 3 4 5 6 |
#!/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
1 |
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.
1 |
vi /etc/systemd/system/myapp.service |
Add
1 2 3 4 5 6 7 8 9 |
[Unit] Description=Sample web server [Service] Type=simple ExecStart=/bin/bash /root/myapp/web.sh [Install] WantedBy=multi-user.target |
Managing Systemd service
First you need to enable the service with
1 |
systemctl enable myapp |
To start the service, run
1 |
systemctl start myapp |
To stop the service, run
1 |
systemctl stop myapp |
To see status of the service, run
1 |
systemctl status myapp |