A pre-built HTTP server binary has been placed on this machine at
/usr/local/bin/simple-server. It listens on port 8080 and, when run, stays
in the foreground until it's killed:
/usr/local/bin/simple-server
Running it straight from the shell works, but it's fragile - closing the terminal kills it, a crash leaves nothing behind to bring it back, and it won't come up after a reboot. That's exactly what systemd can help with.
Your task
Wrap the simple-server binary into a systemd service named simple-server.service so that:
- The unit file lives at
/etc/systemd/system/simple-server.service. - It runs
/usr/local/bin/simple-serveras its main process. - It is enabled (starts automatically after a reboot).
- It is currently active and serving requests on port
8080. - It is automatically restarted when its process terminates abnormally.
Hint 1 - anatomy of a unit file
A minimal systemd service unit has three sections:
[Unit]- human-readable description and ordering hints (e.g.,After=).[Service]- how to start the process:ExecStart=, restart policy, user, etc.[Install]- where to hook the unit when it gets enabled (usuallyWantedBy=multi-user.target).
Refer to man systemd.service and man systemd.unit for the full list of
available directives.
Hint 2 - the systemctl workflow
After writing or editing a unit file under /etc/systemd/system/, systemd
doesn't notice the change automatically. The usual workflow is:
- Reload the systemd manager configuration so it picks up the new file.
- Enable the unit so it will start on boot.
- Start it right now (or combine both with a single
systemctlflag).
Then check its status and recent log output with systemctl status and
journalctl -u.
To verify the restart policy actually works, once the service is up, find its main PID and kill it (e.g., with SIGKILL):
sudo kill -9 $(pgrep -f simple-server)
A properly configured unit will bring the process back up under a new PID within a second or two:
Hint 3 - restart policy
By default, a systemd service that exits - for any reason - stays dead.
The [Service] section has a directive that tells systemd under what
circumstances to bring the process back. The value you pick determines
whether normal exits, failures, or forced kills all trigger a restart.
Take a look at the Restart= options in man systemd.service and pick one
that covers abnormal termination.
The real test of an enabled service is that it comes back on its own after the
machine is restarted. Reboot the server and confirm that simple-server.service
starts again without any manual intervention:
sudo reboot