Skip to content

Instantly share code, notes, and snippets.

@zaxtax
Forked from fcangialosi/EXAMPLE.service
Created April 25, 2024 00:40
Show Gist options
  • Save zaxtax/ced3f7c56a6cd57dd5eeb50207bea42e to your computer and use it in GitHub Desktop.
Save zaxtax/ced3f7c56a6cd57dd5eeb50207bea42e to your computer and use it in GitHub Desktop.
Automatically start long-running script on boot and keep it running with systemd
  1. Create EXAMPLE.service in /etc/systemd/system/

  2. Modify service script as necessary

  • ExecStart is the command to run on startup
  • ExecStop is the command to run on shutdown
  • Restart=always tells systemd to try to restart the script after 30 seconds if it exits for any reason
  1. Tell systemd to start this service automatically when the machine starts:

sudo systemctl enable EXAMPLE.service

  1. Start the service right now (optional):

sudo systemctl start EXAMPLE.service

  1. Check the status (optional):

sudo systemctl status EXAMPLE.service

[Unit]
Description=Description of this service
[Service]
Type=simple
ExecStart=/usr/bin/nohup /path/to/server.sh
Restart=always
RestartSec=30
StandardOutput=/path/to/log
StandardError=/path/to/log
SyslogIdentifier=EXAMPLE
[Install]
WantedBy=multi-user.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment