Skip to content

Instantly share code, notes, and snippets.

@raresteak
Last active September 24, 2024 12:51
Show Gist options
  • Save raresteak/68492b6ffbc2d20c3adbcc5990d32532 to your computer and use it in GitHub Desktop.
Save raresteak/68492b6ffbc2d20c3adbcc5990d32532 to your computer and use it in GitHub Desktop.
Example systemd service file
# myservice.service
[Unit]
Description=Python HTTP server /tmp port 4848
Documentation=man systemd.service
After=network.target #myservice.service requires networking be up first
[Service]
User=sam
ExecStartPre=sh -c 'echo TRUE > /tmp/TRUE'#not required, just an example
ExecStart=/usr/bin/python3 -m http.server -d /tmp 4848
ExecStartPost=touch /tmp/index.html # prevent directory listing, not required, just an example
ExecStop=sh -c '/usr/bin/pkill -f "python3 -m http.server -d /tmp 4848"' #not required, just an example. systemd records the pid at start time and will kill it during stop.
Restart=always # what to do if the process dies
[Install]
WantedBy=multi-user.target # will start when system is in runlevel 2,3,4,5
# more options list in man page
# man systemd.service
# Save myservice.service in /usr/lib/systemd/system/
# systemctl daemon-reload
# systemctl start python3http4848
# systemctl status -l python3http4848
# systemctl stop python3http4848
# systemctl enable python3http4848
# systemctl disable python3http4848
@raresteak
Copy link
Author

raresteak commented Sep 24, 2024

minimum required template for a service that needs networking and system in mult-user mode.

# 
[Unit]
Description=
After=network.target

[Service]
ExecStart=
Restart=always

[Install]
WantedBy=multi-user.target

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment