SystemD

SystemD is used by many Linux Distro. In this article we will discuss how to use SystemD to automatically start an HTTP server.

We can utilize SystemD’s Service to run HTTP server automatically. So we don’t have to run it everytime we start our Computer. First we will need to write script to automatically start the HTTP server.

#!/bin/bash
# file: /etc/systemd/system/start-server.sh

cd /home/user/Downloads
python -m http.server

Next we create SystemD service by creating file-server.service in /etc/systemd/system

[Unit]
Description=Personal file server

Wants=network.target
After=syslog.target network-online.target

[Service]
Type=simple
ExecStart=/usr/local/bin/start-server.sh
Restart=on-failure
RestartSec=10
User=user
Group=user

[Install]
WantedBy=multi-user.target

As you can see in the ExecStart part, we need to put our script on the exact location specified. Afterwards, you can just start it through SystemD with the following command:

systemctl start file-server

If you want it to start automatically on turning on PC, just enable the service with:

systemd enable file-server

That’s all, I hope it’s useful!

Related
Programming · Linux