Skip to content

Instantly share code, notes, and snippets.

@fcangialosi
Created September 2, 2017 20:46
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save fcangialosi/45701b95f7436049a6390fecf3c9b8a1 to your computer and use it in GitHub Desktop.
Save fcangialosi/45701b95f7436049a6390fecf3c9b8a1 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