Skip to content

Instantly share code, notes, and snippets.

@haxpor
Created April 6, 2022 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save haxpor/1b8ded62c2ba72c432fce594242f9567 to your computer and use it in GitHub Desktop.
Save haxpor/1b8ded62c2ba72c432fce594242f9567 to your computer and use it in GitHub Desktop.
Guideline and note on crafting out a minimal long running process via systemd config file (sweat and tears in the process)
[Unit]
Description=My systemd service
After=network.target
# allow cap of 4 attempts to start the process
# within 1 second, if not success, then give up
StartLimitIntervalSec=1
StartLimitBurst=4
[Service]
Type=simple
User=<supply-user-here>
ExecStart=/always/use/the/absolute/path/command --something --another-flag
# we won't be able to fail the systemd process with this line
# as it is executed inside `bash -c`. But this will allow us to
# pipe, otherwise output of command will be shown in `StandardOutput` e.g. journal.
ExecStartPost=bash -c 'curl -s "https://something.com" > /dev/null'
# better use ExecStopPost in order to cover both normal restart of process, and
# unexpected restart (process crashes). ExecStop= only gets executed when
# ExecStart= successfully starts only, otherwise it won't get executed.
ExecStopPost=some-command
StandardOutput=journal
Restart=always
# recommended to use this instead of Environment= or various others
# due to security. This one strikes balance in ease of use, and maintain
# security. If you `systemctl show <service>`, it will output all environment
# variables as defined inside systemd config file, but not for EnvironmentFile=.
# As well, we can set permission accordingly for our envrionment variable file.
# So only us can read. Note, these variables will be passed before process runs.
EnvironmentFiles=/home/your-username/env-files/my-env-files.conf
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment