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

26 thoughts to “How to run a Linux Program on Startup”

  1. Thanks for this Tim – your articles helped me getting homebridge working perfectly! I was so lost without these articles – really appreciate it.

    I’m trying to take these learnings and get the alexa > sonos node.js service running explained here

    https://github.com/rgraciano/echo-sonos#get-jishis-node-sonos-http-api-working

    They give not so detailed instructions on how to get it up and running..

    Optional: You probably want to set this up to auto-start in the event it dies. Auto-launch is one of many solutions to make that happen. – https://www.npmjs.com/package/auto-launch

    As you may guess i’m completely lost!

    1. This is what I created – assuming I usually go to the folder home/pi/node_modules/sonos-http-api/
      then just type “npm start” and it starts..

      Description=Sonos Alexa Node Server

      Wants=network.target
      After=syslog.target network-online.target

      [Service]
      Type=simple
      ExecStart=home/pi/node_modules/sonos-http-api/npm start
      Restart=on-failure
      RestartSec=10
      KillMode=process

      [Install]
      WantedBy=multi-user.target

      1. Thanks glad they have helped! I think you would want your command to first change to the directory then run npm start.

        You could also try npm start –prefix path/to/your/app

  2. ExecStart=YOUR_COMMAND_HERE

    What command is this? No one says what this command is supposed to be. Am I supposed to just “know” what goes there?

  3. Thank you for this straight-forward article – it’s exactly what I’ve been searching for. I’ve written a daemon process in C but was having trouble with other complicated instructions in getting it to run as a system process. This worked first time!
    I hope you can understand the simple delight of an engineer of some thirty years experience on seeing a System OK LED start flashing on boot!

    Best regards,
    Peter

  4. This article has made a local hero at my place of work. Thanks a lot.

    Here is mine
    ==========================================
    Description=JBOSS_EAP_7.1.0_startup_service

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=/root/EAP-7.1.0/bin/standalone.sh
    Restart=on-failure
    RestartSec=10
    KillMode=process

    [Install]
    WantedBy=multi-user.target

  5. Hi Tim,
    I tried your method on my application program on Debian OS. It is not working for me.
    My program is a GUI based application program, which is located in the folder of /home/linaro/. I run my application from Terminal is as below:
    ./my_app
    It works fine.

    Below is the service file my_app.service, which is stored in the folder of /ect/ststemd/system/
    Description=My_main_app

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=/home/linaro/my_app
    Restart=on-failure
    RestartSec=10
    KillMode=process

    [Install]
    WantedBy=multi-user.target

    It failed on start the service.
    The systemctl log is as below:
    [/etc/system/system/my_app.service:1] Assignment outside of section. Ignoring.
    [/etc/system/system/my_app.service:3] Assignment outside of section. Ignoring.
    [/etc/system/system/my_app.service:4] Assignment outside of section. Ignoring.
    [/etc/system/system/my_app.service:6] Unknown section ‘service’. Ignoring.
    my_app.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.

    Would you please let me know what’s wrong with it?
    Thanks.

  6. Unfortunetly, this doesn’t work for me. I have the following service setup:
    Description=xbindkeys packege

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=/usr/bin/xbindkeys
    Restart=on-failure
    RestartSec=10
    KillMode=process

    [Install]
    WantedBy=multi-user.target

    But at boot nothing happens.

  7. Hi Tim,
    so if my Server.js file is in home/ubuntu/backend/Server.js my EXECSTART should be home/ubuntu/backend/node Server.js?

  8. Hi Tim,
    i want to run a startup code for ZED board how could i do because in zed board there is no system and bashrc

  9. Hi Tim – Been trying this method to get HomeBridge to launch on start vs. your prior post. I keep getting an error message when I start the service:

    [email protected]:~ $ sudo systemctl start home
    Failed to start home.service: Unit home.service is not loaded properly: Invalid argument.
    See system logs and ‘systemctl status home.service’ for details.

    When I go into the Log this is what I see:
    ● home.service
    Loaded: error (Reason: Invalid argument)
    Active: inactive (dead)

    Jun 17 08:37:15 raspberrypi systemd[1]: [/etc/systemd/system/home.service:1] Assignment outside of section. Ignoring.
    Jun 17 08:37:15 raspberrypi systemd[1]: [/etc/systemd/system/home.service:3] Assignment outside of section. Ignoring.
    Jun 17 08:37:15 raspberrypi systemd[1]: [/etc/systemd/system/home.service:4] Assignment outside of section. Ignoring.
    Jun 17 08:37:15 raspberrypi systemd[1]: [/etc/systemd/system/home.service:8] Executable path is not absolute, ignoring: homebridge
    Jun 17 08:37:15 raspberrypi systemd[1]: home.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.

    The command is as follows:

    Description=Home

    Wants=network.target
    After=syslog.target network-online.target

    [Service]
    Type=simple
    ExecStart=homebridge
    Restart=on-failure
    RestartSec=10
    KillMode=process

    [Install]
    WantedBy=multi-user.target

      1. Thanks. That sort of worked… It launches Homebridge on start. But it seems like its launches a different instance / config file than I setup in my user (pi). Do I fix that by changing the revised path (/usr/bin/homebridge)?

          1. Sorry for the stupid questions, but this is all new to me. If the user is “pi”, how would I update the path? the path “/usr/bin/homebridge” launched a version of Homebridge that didn’t have my config file.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.