How to run a Linux Program on Startup

Here are the steps to have a program or script start on boot on a linux machine using Systemctl. I’m currently using this start several services on my raspberry pi. DigitalOcean wrote an article that goes into more detail on Systemctl.

  1. Run this command
    sudo nano /etc/systemd/system/YOUR_SERVICE_NAME.service
  2. Paste in the command below. Press ctrl + x then y to save and exit
    Description=GIVE_YOUR_SERVICE_A_DESCRIPTION
    
    Wants=network.target
    After=syslog.target network-online.target
    
    [Service]
    Type=simple
    ExecStart=YOUR_COMMAND_HERE
    Restart=on-failure
    RestartSec=10
    KillMode=process
    
    [Install]
    WantedBy=multi-user.target
  3. Reload services

    sudo systemctl daemon-reload
  4. Enable the service

    sudo systemctl enable YOUR_SERVICE_NAME
  5. Start the service
    sudo systemctl start YOUR_SERVICE_NAME
  6. Check the status of your service
    systemctl status YOUR_SERVICE_NAME
  7. Reboot your device and the program/script should be running. If it crashes it will attempt to restart

Also published on Medium.

Leave a Reply

Your email address will not be published. Required fields are marked *