$ # Clone healthcheck repository
$ cd ~
$ git clone https://github.com/wingkeet/healthcheck.git
$ # Create Python virtual environment and install libraries
$ cd healthcheck
$ python3 -m venv venv
$ source venv/bin/activate
$ pip install requests
$ deactivate
$ # Install new-style daemon for systemd
$ sudo cp healthcheck.service /etc/systemd/system
$ sudo systemctl daemon-reload
$ sudo systemctl start healthcheck
$ journalctl -f -u healthcheck
$ sudo systemctl stop healthcheck
The information presented here was extracted from Linux System Programming Techniques by Jack-Benny Persson and published by Packt.
We create a new-style daemon for systemd. The old forking type is referred to as a SysV-style daemon.
Daemons that are handled by systemd don't need to fork or close their file descriptors. Instead, it's advised to use standard output and standard error to write the daemon's logs to the systemd journal.
We need to flush stdout. Normally, streams are line-buffered, meaning they get flushed on each new line. But when stdout is redirected to something else, like with systemd, it's instead fully buffered.