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.
- Run this command
sudo nano /etc/systemd/system/YOUR_SERVICE_NAME.service
- 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
-
Reload services
sudo systemctl daemon-reload
-
Enable the service
sudo systemctl enable YOUR_SERVICE_NAME
- Start the service
sudo systemctl start YOUR_SERVICE_NAME
- Check the status of your service
systemctl status YOUR_SERVICE_NAME
- Reboot your device and the program/script should be running. If it crashes it will attempt to restart
Thanks for reading. Make sure you follow me on Twitter to stay up to date on the progress of my side projects T.LY, Weather Extension, and Link Shortener Extension. If you are interested in the tech I use daily, check out my uses page.
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!
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
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
Thanks for this article Tim.
Sorry tehat I bother you.
I’m making a service unit file that starts a Firefox script on boot. (I’m using Ubuntu)
I’m need your help, please contact me to fix this unit.
Opening web browser on boot
I thought I might be able to get this done by making a service unit file that starts a firefox script on boot.
Description=”Run firefox at boot”
Wants=network.target
After=syslog.target network-online.target
[Service]
Type=simple ExecStart=/usr/bin/google (“#!/bin/bash su ‘username’ -c /usr/bin/firefox-stable)
Restart=on-failure
RestartSec=10
KillMode=process
[Install]
WantedBy=multi-user.target
Hey Leo,
I don’t think you need the /usr/bin/google part
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?
YOUR_COMMAND_HERE can be whatever command you want to run on startup
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
That’s great to hear!! Glad it helped
When i try to boot .net core app, it’s not working fully, any other way?
Are you seeing any errors?
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
That’s awesome! Glad it helped.
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.
Is there a formatting issue with the service file?
I checked again. I can’t find out any formatting issue.
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.
Do you see any errors?
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?
Is home your user home folder? May need to be the full path.
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
No sure about a ZED board. What operating system is I running ?
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:
pi@raspberrypi:~ $ 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
Hey John, try adding the full path to the homebridge command. You should be able to use whereis homebridge
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)?
Yes that should fix it
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.
Try /home/pi
Hello Tim, I tried setting up pCloud on Ubuntu 16.04 with the walkthrough a few days ago, but something’s wrong and I don’t understand what it’s trying to tell me. Just to be clear, it has not been able to launch pCloud once since I set it up like this. I just didn’t find the time to post about it here. “ultra” is the name of my system.
“`
$ systemctl status pcloud
● pcloud.service
Loaded: loaded (/etc/systemd/system/pcloud.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Sam 2019-06-22 11:48:12 CEST; 1s ago
Process: 8534 ExecStart=/home/thymaro/opt/pcloud/pcloud (code=exited, status=1/FAILURE)
Main PID: 8534 (code=exited, status=1/FAILURE)
CGroup: /system.slice/pcloud.service
Jun 22 11:48:12 ultra systemd[1]: pcloud.service: Unit entered failed state.
Jun 22 11:48:12 ultra systemd[1]: pcloud.service: Failed with result ‘exit-code’.
~
~
~
“`
First, how do you format code in this reply box?
Maybe this helps identifying the problem:
*************************************
Description=Starts the pCloud service on startup.
Wants=network.target
After=syslog.target network-online.target
[Service]
Type=simple
ExecStart=/home/thymaro/opt/pcloud/pcloud
Restart=on-failure
RestartSec=10
KillMode=process
[Install]
WantedBy=multi-user.target
*************************************
@TIM,
i’m using a linux as well.
Hi Tim,
I’m new to linux, python3 & Raspberry Pi. My forte is Windows & Visual Basic. I’m
having problems setting up a service for the following.
“/home/pi/test” – This is the script I want to run after the Raspberry Pi boots.
It ensures the current directory is ‘/home/pi’ (for system data logging), then
starts a terminal and runs a python3 program that monitors home system status
(Network & AC power). The scripts are executable and work from a terminal and
the GUI, and I’ve tried it both ways below:
*********************************************************************
#!/bin/sh
#
chdir /home/pi
/usr/bin/x-terminal-emulator -e /usr/bin/python3 /home/pi/HomeStat.py
*********************************************************************
#!/bin/sh
#
cd /home/pi
x-terminal-emulator -e python3 HomeStat.py
**********************************************************************
“HomeStat.service” – The following is the service I set up per your instructions
**********************************************************************
Description=Home_Status_Monitor
Wants=network.target
After=syslog.target network-online.target
[Service]
Type=simple
ExecStart=/home/pi/test
Restart=on-failure
RestartSec=10
KillMode=process
[Install]
WantedBy=multi-user.target
*************************************************************************
This is the status message I received
*************************************************************************
● HomeStat.service
Loaded: loaded (/etc/systemd/system/HomeStat.service; enabled; vendor preset: enabled)
Active: activating (auto-restart) (Result: exit-code) since Thu 2019-07-18 19:18:23 EDT; 9s ago
Process: 1587 ExecStart=/home/pi/test (code=exited, status=1/FAILURE)
Main PID: 1587 (code=exited, status=1/FAILURE)
Jul 18 19:18:23 raspberrypi systemd[1]: HomeStat.service: Unit entered failed state.
Jul 18 19:18:23 raspberrypi systemd[1]: HomeStat.service: Failed with result ‘exit-code’.
*************************************************************************
I don’t understand what the status message is saying! Where do I find out information on these
status messages? What is status=1/FAILURE? Is there a listing of these error
codes with a breakdown or synopsis of the failure. Any help would be appreciated.
Thanks,
Joe
Hey Joe,
Does chdir work? I believe that is a Windows command to change directories.
Hi Tim,
chdir & cd both change the working directory in Linux and MSDOS. One command both systems agree on, but the path separators are different (ie. ‘/’ or ‘\’). As a note, I went looking for the man page for cd & chdir, and found that there is one for chdir, but there isn’t one (at least it wasn’t on my copy of Raspbian, ver. 4.14, stretch) for cd. However, both work. Missing documentation or Magic?
I went looking for a full path on both chdir & cd, but found no routine in any Linux directory (to add this to the command in my ‘test’ script). They must be internal commands. I’ve run out of things to try. Still looking…
By the way, where did they hide information on these error codes?
Thanks,
Joe
Hi Tim,
Just to check myself, I just now cranked up my Pi and tested cd and chdir. I was half right and half wrong. cd works to change directories, chdir does not! However, there IS a man page for chdir & fchdir (change the working directory), but NONE for cd? It must be spam…
Now just as a note, my original script used cd instead of chdir. The results were as displayed in my original email. I actually changed the script to read chdir just before I sent in the email, because I found a man page for chdir. I did run the script with both cd & chdir and got failures, however the status message above was from the original test (using cd). Sorry for the confusion (mostly mine). After I changed the script to chdir, I ran the script from the GUI (click & ‘execute’) and it ran! So I figured both cd & chdir were the same. I was wrong.
Thanks,
Joe
Hi Tim,
I found out what caused the failure while trying to set up the systemd.service. While scouring through the technical documents found on the Internet, I found that to set up a systemd unit, the script that you want to run must not be interactive (found on Red Hat). Since I was trying to start a terminal to display current status (Home Status), systemd didn’t like that. I had to remove the terminal from my startup script, and add data-logging to my Home Status program. It now works fine.
Thanks,
Joe
Great! Glad you got it to work. Thanks for sharing the additional tip.
Hi Tim,
This guide works perfect for me and is exactly what I was looking for.
Thanks a lot
Great!
Hello Tim and thanks, i’m having an error
Loaded: loaded (/etc/systemd/system/fakeSmtp.service; enabled)
Active: activating (auto-restart) (Result: exit-code) since Fri 2020-04-03 20:22:14 CEST; 2s ago
Process: 20720 ExecStart=/var/docker/mailhog docker-compose up -d (code=exited, status=203/EXEC)
Main PID: 20720 (code=exited, status=203/EXEC)
*************************************************
Description=starting MailHog with docker to intercept mails
Wants=network.target
After=syslog.target network-online.target
[Service]
Type=simple
ExecStart=/var/docker/mailhog docker-compose up -d
Restart=on-failure
RestartSec=10
KillMode=process
[Install]
WantedBy=multi-user.target
Did i do something wrong ?
Hello, I´m Paulo.
I created a program on linux and used the Makefile to compiled.
I tried use your method but do not work. I check the status and is runnig (light is green), but I try to use and nothing. I believe because the way to execute.
ExecStart=/root/Documents/Automation/transmitter
That at all ? I need put just that ?
Hello sometimes due to permissions programs cannot run. What user are you using when you run it?
Hi TIM,
Thanks for helping so many professionals. Like others in our project we work an application called IICS and to start IICS service we created IICS user which starts all IICS services but on linux VM if it reboot we have to manually start the services but fter going through your Article I was able to bring all IICS services up on system reboot but the problem I faced it started all IICS service with root user which created lot of issues and I have to disable the service .
Appreciate your help here .How can I start IICS service using systemctl with IICS user not root.
Hi Tim, I am stuck like many others with the ExecStart. I have a simple c program that takes inputs from user and displays something. I built it with gcc and it works like ./myprogram. Now what should I put in ExecStart and will it display on the console and accept keyboard inputs? How do I do that?