Challenge, Easy,  on  Linux

Create a systemd Service for a Long-Running Process

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:

  1. The unit file lives at /etc/systemd/system/simple-server.service.
  2. It runs /usr/local/bin/simple-server as its main process.
  3. It is enabled (starts automatically after a reboot).
  4. It is currently active and serving requests on port 8080.
  5. 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 (usually WantedBy=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:

  1. Reload the systemd manager configuration so it picks up the new file.
  2. Enable the unit so it will start on boot.
  3. Start it right now (or combine both with a single systemctl flag).

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