[ale] writing a demo systemd service

Steve Litt slitt at troubleshooters.com
Wed Jul 20 16:10:39 EDT 2022


On Wed, 2022-07-20 at 11:06 -0500, Leam Hall via Ale wrote:
> Anyone know enough systemd to help me write a demo service? It needs to be simple
> enough to explain in 15 minutes, run a python based httpd server, and detailed
> enough to point towards best practices. It will run on RHEL 7 and 8, here's what I
> have so far:
> 
> ### file: /etc/systemd/system/pyhttpd.service
> #
> # [Unit]
> # Description=Python based HTTPD
> # After=network.target
> #
> # [Service]
> # Type=simple
> # ExecStart=/usr/local/bin/httpd_server.py
> # Restart=on-failure
> 
> I realize there are many options (non-systemd, non-python httpd), but this is what
> I have and I need to show others how to do it.
> 
> Thanks!
> 
> Leam

Looks reasonable to me, assuming httpd_server.py does NOT background itself. I'd
change the "Restart=" to "always" because I assume you want it to restart no matter
what stopped it. It's too bad you can't and/or/xor the Restart= stuff so you could
restart in most, but not all instances, but that's the price you pay for key=>value
pairs instead of simple scripts.

I'd look into the differences between After=, Wants=, and Requires= . I'd also look
into the "needs" and "provides" stuff.

By the way, I think your unit file runs httpd_server.py as root. If this isn't what
you want, look into how to make it run as a different user/group.

Why don't you just try the unit file you have? It will probably work.

SteveT

PS: When you're all done:

#!/bin/sh
if ping -c1 8.8.8.8; then
  exec /usr/local/bin/httpd_server.py
else
  sleep 1
fi

:-)


More information about the Ale mailing list