-
- Posts: 12
- Joined: Thu Aug 23, 2012 8:42 pm
- Location: Sheffield, UK
Auto running a program after boot
Loading the pi to the command line , what do I alter to make the pi run a program automatically
Can anyone help
Can anyone help
Re: Auto running a program after boot
If you are using Raspbian Jessie, which you should be, then getting a program to run at boot up is very easy using the systemd initialization system.
Example:
I have a program called "propanel" that I want to start at boot time. First I write a little text file that tells systemd how to run the program. It looks like this:
That file is clear enough. It specifies the working directory to run your program in, the command required to start it, and so on.
Put that file into /etc/systemd/system/propanel.service. Make sure it is readable/writeable/executable by root:
Now enable the service with a systemd command:
And it will be run at boot time.
You can start it without rebooting with:
If that fails for some reason you will get a nice error message. And usually you have to look in the logs to see what systemd did not like about your service file "cat /var/log/syslog"
You can stop your service with:
There is much on the net written about this. For example: http://unix.stackexchange.com/questions ... or-systemd
Many people hate systemd for whatever reasons but I find this method of getting things running much nicer than the old script hacking days.
Example:
I have a program called "propanel" that I want to start at boot time. First I write a little text file that tells systemd how to run the program. It looks like this:
Code: Select all
[Service]
WorkingDirectory=/home/pi/propanel
ExecStart=node /home/pi/propanel/propanel.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=propanel
User=root
Group=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
Put that file into /etc/systemd/system/propanel.service. Make sure it is readable/writeable/executable by root:
Code: Select all
$ sudo cp propanel.service /etc/systemd/system/
$ sudo chmod u+rwx /etc/systemd/system/propanel.service
Code: Select all
$ sudo systemctl enable propanel
You can start it without rebooting with:
Code: Select all
sudo systemctl start propanel
You can stop your service with:
Code: Select all
sudo systemctl stop propanel
Many people hate systemd for whatever reasons but I find this method of getting things running much nicer than the old script hacking days.
Last edited by Heater on Sun Jan 08, 2017 10:26 am, edited 1 time in total.
Slava Ukrayini.
- DougieLawson
- Posts: 42483
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
Re: Auto running a program after boot
Thanks for posting that clear and concise way to get systemd services running. It's a bunch easier than hacking /etc/init.d/skeleton.Heater wrote: Many people hate systemd for whatever reasons but I find this method of getting things running much nicer than the old script hacking days.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
-
- Posts: 12
- Joined: Thu Aug 23, 2012 8:42 pm
- Location: Sheffield, UK
Re: Auto running a program after boot
Thanks , I'll give it a try on my pi 2 as a minecraft server
-
- Posts: 12
- Joined: Thu Aug 23, 2012 8:42 pm
- Location: Sheffield, UK
Re: Auto running a program after boot
How do you pass parameters within this script say for example sudo java -Xmx512 -Xms990 -jarfile spigot.jar nogui
Thanks for your help
Thanks for your help
Re: Auto running a program after boot
I usually use a "cron" job scheduled for the "@reboot" time. This is a "nonstandard" cron extension but seems to work on every Linux system since long ago. Read "man 5 crontab".
Re: Auto running a program after boot
Retrogadgets,
How do you pass parameters within this script say for example sudo java -Xmx512 -Xms990 -jarfile spigot.jar nogui
Ah yes. I lied above. To make it simple. My actual service file looks like this:
As you see the "ExecStart" start line actually starts the node.js interpreter at /usr/local/bin/node. And passes it the parameters "--expose-gc" and "/home/pi/propanel/assets/server-bundle.js"
The later of which is the JS program I actually want to run.
Your java problem is much the same except it java instead of node.
You don't need the "sudo" by the way.
How do you pass parameters within this script say for example sudo java -Xmx512 -Xms990 -jarfile spigot.jar nogui
Ah yes. I lied above. To make it simple. My actual service file looks like this:
Code: Select all
[Service]
WorkingDirectory=/home/pi/propanel
ExecStart=/usr/local/bin/node --expose-gc /home/pi/propanel/assets/server-bundle.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=propanel
User=root
Group=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
The later of which is the JS program I actually want to run.
Your java problem is much the same except it java instead of node.
You don't need the "sudo" by the way.
Slava Ukrayini.
Re: Auto running a program after boot
Hi, I'm new to RPi "world" and I'm trying to do something similar to Retrogadgets.
I have developed a java application which I can call via terminal 'java -jar /home/pi/eDeiaCall/eDeiaCall.jar'. Now I'm struggling to run it on boot. I've tried your approach but without success.
When I use this
ExecStart=/home/pi/eDeiaCall/eDeiaCall.jar
I get
"Apr 29 12:36:44 raspberrypi systemd[1590]: Failed at step EXEC spawning /home/pi/eDeiaCall/eDeiaCall.jar: Exec format error
Apr 29 12:36:44 raspberrypi systemd[1]: edeiacall.service: main process exited, code=exited, status=203/EXEC"
So I decided to try
ExecStart=java -jar /home/pi/eDeiaCall/eDeiaCall.jar
And I'm getting this error:
"Failed to start edeiacall.service: Unit edeiacall.service failed to load: Invalid argument. See system logs and 'systemctl status edeiacall.service' for details."
What am I doing wrong? I'm sorry if this is to obvious for you, but like I said previously Linux/RPi is new to me.
Thank you in advance.
I have developed a java application which I can call via terminal 'java -jar /home/pi/eDeiaCall/eDeiaCall.jar'. Now I'm struggling to run it on boot. I've tried your approach but without success.
When I use this
ExecStart=/home/pi/eDeiaCall/eDeiaCall.jar
I get
"Apr 29 12:36:44 raspberrypi systemd[1590]: Failed at step EXEC spawning /home/pi/eDeiaCall/eDeiaCall.jar: Exec format error
Apr 29 12:36:44 raspberrypi systemd[1]: edeiacall.service: main process exited, code=exited, status=203/EXEC"
So I decided to try
ExecStart=java -jar /home/pi/eDeiaCall/eDeiaCall.jar
And I'm getting this error:
"Failed to start edeiacall.service: Unit edeiacall.service failed to load: Invalid argument. See system logs and 'systemctl status edeiacall.service' for details."
What am I doing wrong? I'm sorry if this is to obvious for you, but like I said previously Linux/RPi is new to me.

Thank you in advance.
-
- Posts: 43
- Joined: Sat Mar 03, 2012 3:49 pm
Re: Auto running a program after boot
Nothing in here, but thank you.Heater wrote:If you are using Raspbian Jessie, which you should be, then getting a program to run at boot up is very easy using the systemd initialization system.
Example:
I have a program called "propanel" that I want to start at boot time. First I write a little text file that tells systemd how to run the program. It looks like this:
That file is clear enough. It specifies the working directory to run your program in, the command required to start it, and so on.Code: Select all
[Service] WorkingDirectory=/home/pi/propanel ExecStart=/home/pi/propanel/propanel.js Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=propanel User=root Group=root Environment=NODE_ENV=production [Install] WantedBy=multi-user.target
Put that file into /etc/systemd/system/propanel.service. Make sure it is readable/writeable/executable by root:
Now enable the service with a systemd command:Code: Select all
$ sudo cp propanel.service /etc/systemd/system/ $ sudo chmod u+rwx /etc/systemd/system/propanel.service
And it will be run at boot time.Code: Select all
$ sudo systemctl enable propanel
You can start it without rebooting with:If that fails for some reason you will get a nice error message. And usually you have to look in the logs to see what systemd did not like about your service file "cat /var/log/syslog"Code: Select all
sudo systemctl start propanel
You can stop your service with:There is much on the net written about this. For example: http://unix.stackexchange.com/questions ... or-systemdCode: Select all
sudo systemctl stop propanel
Many people hate systemd for whatever reasons but I find this method of getting things running much nicer than the old script hacking days.
-
- Posts: 84
- Joined: Tue Nov 10, 2015 3:06 pm
Re: Auto running a program after boot
Thanks
The fix worked. If I wanted to run an additional program what then?
The fix worked. If I wanted to run an additional program what then?
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.
All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.
All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.
Re: Auto running a program after boot
How about just creating another service file for your other program.
Slava Ukrayini.
Re: Auto running a program after boot
Heater wrote: ↑Sun Mar 06, 2016 5:47 amIf you are using Raspbian Jessie, which you should be, then getting a program to run at boot up is very easy using the systemd initialization system.
Example:
I have a program called "propanel" that I want to start at boot time. First I write a little text file that tells systemd how to run the program. It looks like this:
That file is clear enough. It specifies the working directory to run your program in, the command required to start it, and so on.Code: Select all
[Service] WorkingDirectory=/home/pi/propanel ExecStart=node /home/pi/propanel/propanel.js Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=propanel User=root Group=root Environment=NODE_ENV=production [Install] WantedBy=multi-user.target
Put that file into /etc/systemd/system/propanel.service. Make sure it is readable/writeable/executable by root:
Now enable the service with a systemd command:Code: Select all
$ sudo cp propanel.service /etc/systemd/system/ $ sudo chmod u+rwx /etc/systemd/system/propanel.service
And it will be run at boot time.Code: Select all
$ sudo systemctl enable propanel
You can start it without rebooting with:If that fails for some reason you will get a nice error message. And usually you have to look in the logs to see what systemd did not like about your service file "cat /var/log/syslog"Code: Select all
sudo systemctl start propanel
You can stop your service with:There is much on the net written about this. For example: http://unix.stackexchange.com/questions ... or-systemdCode: Select all
sudo systemctl stop propanel
Many people hate systemd for whatever reasons but I find this method of getting things running much nicer than the old script hacking days.
Does the first bit of code need to be in a text editor file and what does it need to be saved as.
Also does it work with a python code if not please could you lead me to tutorial where it does.
Additionally, does it work if the python code is in a load of other directories not the base one.
Thanks.
- davidcoton
- Posts: 7005
- Joined: Mon Sep 01, 2014 2:37 pm
- Location: Cambridge, UK
Re: Auto running a program after boot
Q1, yes:willbobby wrote: Does the first bit of code need to be in a text editor file and what does it need to be saved as, and does it work with a python code if not please could you lead me to tutorial where it does.
Q2, you obviously need to adapt the file, for example " ExecStart=python3 /home/pi/pythonprogs/prog.py"Heater wrote: Put that file into /etc/systemd/system/propanel.service. Make sure it is readable/writeable/executable by root:Code: Select all
$ sudo cp propanel.service /etc/systemd/system/ $ sudo chmod u+rwx /etc/systemd/system/propanel.service
Try it, post your attempt here when you get stuck!
Please test your code first (without autostart), it's very embarrassing to find you have auto-started a program that runs an infinite loop (think I've got the T shirt somewhere).
Location: 345th cell on the right of the 210th row of L2 cache
Re: Auto running a program after boot
I have done everything as told but for some reason the code doesn't run after the boot up.
My code is:
I know for definite that the code runs perfectly.
My code is:
Code: Select all
[service]
WorkingDirectory=/home/pi/Desktop/Model_Phalanx/Adafruit_PWM_Servo/Servo_Keyboard
ExecStart=python3 /home/pi/Desktop/Model_Phalanx/Adafruit_PWM_Servo/Servo_Keyboard.py
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=Servo_Keyboard
User=root
Group=root
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
- DougieLawson
- Posts: 42483
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
Re: Auto running a program after boot
Lose this line:
Lose this line:
Run these commands
sudo systemctl disable xyzzy.service
sudo systemctl enable xyzzy.service
sudo systemctl start xyzzy.service
sudo journalctl -xe
with the name of your service file rather than xyzzy.service.
Code: Select all
WorkingDirectory=/home/pi/Desktop/Model_Phalanx/Adafruit_PWM_Servo/Servo_Keyboard
Code: Select all
Environment=NODE_ENV=production
sudo systemctl disable xyzzy.service
sudo systemctl enable xyzzy.service
sudo systemctl start xyzzy.service
sudo journalctl -xe
with the name of your service file rather than xyzzy.service.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
Re: Auto running a program after boot
I have a Raspberry Pi Zero based, remote, battery powered, time-lapse photography project with a focus on low power.
The key to serious power savings is to power down both my RPiZero and the Canon camera it controls, in between shots of an extended time-lapse session (think weeks / months).
I will use a STM32 microprocessor to supply power to both devices at the correct interval (ie. every 5 or 10 minutes), upon which, the PiZero will control the camera to take an image, transfer it to the Pi, resize it and upload it to my webspace, then both devices will shutdown.
My question here is, if I set up a bash script to run the camera and image processing and then to shutdown the PiZero. How would I be able to interrupt this cycle, when I need to change location and interval settings etc. I once created an infinite loop like this and was unable to stop the process because the @reboot cron script ran before my ability to interrupt it. Therefore the box shutdown and I was back at square 1.
I'm hoping I might be able to configure an 'IF' statement in the start up script to check whether I'd applied a jumper to a couple of GPIO pins before proceeding with the rest of the script but I'm not finding an answer among the similar start up script questions that abound on the Internet/
Cheers
Stack
The key to serious power savings is to power down both my RPiZero and the Canon camera it controls, in between shots of an extended time-lapse session (think weeks / months).
I will use a STM32 microprocessor to supply power to both devices at the correct interval (ie. every 5 or 10 minutes), upon which, the PiZero will control the camera to take an image, transfer it to the Pi, resize it and upload it to my webspace, then both devices will shutdown.
My question here is, if I set up a bash script to run the camera and image processing and then to shutdown the PiZero. How would I be able to interrupt this cycle, when I need to change location and interval settings etc. I once created an infinite loop like this and was unable to stop the process because the @reboot cron script ran before my ability to interrupt it. Therefore the box shutdown and I was back at square 1.
I'm hoping I might be able to configure an 'IF' statement in the start up script to check whether I'd applied a jumper to a couple of GPIO pins before proceeding with the rest of the script but I'm not finding an answer among the similar start up script questions that abound on the Internet/
Cheers
Stack
Re: Auto running a program after boot
If you've got wiringpi installed (it usually is), you could add the following into your bash script
Then put your jumper on the two pins (39 and 40) of the GPIO header nearest the USB sockets. You could use another pair of pins where one of them is 0v/GND, but using the last two makes it easier to locate.
Code: Select all
# Check to see if pins 39 and 40 are jumpered
# Set Pin 40 to input with pull-up (BCM 21)
gpio -g mode 21 up
# If there is a jumper this GPIO will read low, otherwise it will read high
jumper="$(gpio -g read 21)"
if [ "$jumper" = "1" ]
then
# No jumper in place, run code as normal
echo "No jumper"
else
# There is a jumper, don't do anything
echo "Jumper in place."
fi
Re: Auto running a program after boot
rpdom,
You're a champ.. that's exactly what I was hoping for.. wasn't sure it was possible.
Cheers and have a fantastic weekend
Sdack
You're a champ.. that's exactly what I was hoping for.. wasn't sure it was possible.
Cheers and have a fantastic weekend
Sdack
Re: Auto running a program after boot
my program name app.js . I want to run when pi boot up . The above text file code of propanal, by what extension we have to in etc/systemd/system/propanal.service..
we have save the above code by .service extension
we have save the above code by .service extension
Re: Auto running a program after boot
Stupid question...
... to disable running the file after boot do you just delete the *.service file? No other housekeeping required ?
... to disable running the file after boot do you just delete the *.service file? No other housekeeping required ?
- DougieLawson
- Posts: 42483
- Joined: Sun Jun 16, 2013 11:19 pm
- Location: A small cave in deepest darkest Basingstoke, UK
Re: Auto running a program after boot
systemctl disable xyzzy.service
No need to delete anything.
No need to delete anything.
Languages using left-hand whitespace for syntax are ridiculous
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
DMs sent on https://twitter.com/DougieLawson or LinkedIn will be answered next month.
Fake doctors - are all on my foes list.
The use of crystal balls and mind reading is prohibited.
-
- Posts: 145
- Joined: Mon Jul 30, 2018 3:44 pm
Re: Auto running a program after boot
I tried this for starting a node.js script which uses mysql module, and its failing, requiring me to manually stop and start again once the desktop is loaded before it works.... Im pretty sure I need to figure out how to set it to start after the mariaDB service has started...
So how do I accomplish that?
So how do I accomplish that?
-
- Posts: 145
- Joined: Mon Jul 30, 2018 3:44 pm
Re: Auto running a program after boot
I figured it out... sharing just in case someone else stumbles across this thread.grininmonkey wrote: ↑Mon Jul 30, 2018 9:14 pmI tried this for starting a node.js script which uses mysql module, and its failing, requiring me to manually stop and start again once the desktop is loaded before it works.... Im pretty sure I need to figure out how to set it to start after the mariaDB service has started...
So how do I accomplish that?
I am experimenting with using a simple node.js script to act as a JSON server for a web site which polls data that my pi is logging... so it requires mysqld to be up and running before it runs..... so after some research I found out about the wants, require and after parameters for a [Unit] tag. The following service file ended up working for me.
[Unit]
Description = node_json_requests
After = network.target mysqld.service
[Service]
ExecStart=/usr/bin/node --expose-gc /home/pi/Projects/Services/node_json_requests.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=node_json_requests
User=root
Group=root
[Install]
WantedBy=multi-user.target
Re: Auto running a program after boot
Still struggling with this, the new code is:
Permissions set to 755
error report:
It seems to be a problem with from the above I think, but can't work out how to solve it.
Code: Select all
[Unit]
Description=Loxone log
After=network.target
[Service]
WorkingDirectory=/home/pi/loxone-stats-influx/loxone-ws-influx
ExecStart=/home/pi/n/bin/npm start
Restart=always
User=root
Group=root
[Install]
#WantedBy=multi-user.target
error report:
Code: Select all
pi@raspberrypi:~ $ sudo systemctl disable loxone_log.service
pi@raspberrypi:~ $ sudo systemctl daemon-reload
pi@raspberrypi:~ $ sudo systemctl start loxone_log
pi@raspberrypi:~ $ sudo journalctl -xe
Jan 22 22:27:43 raspberrypi systemd[1]: Stopped Loxone log.
-- Subject: Unit loxone_log.service has finished shutting down
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- Unit loxone_log.service has finished shutting down.
Jan 22 22:27:43 raspberrypi systemd[1]: Started Loxone log.
-- Subject: Unit loxone_log.service has finished start-up
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- Unit loxone_log.service has finished starting up.
--
-- The start-up result is done.
Jan 22 22:27:43 raspberrypi npm[5505]: /usr/bin/env: ‘node’: No such file or directory
Jan 22 22:27:43 raspberrypi systemd[1]: loxone_log.service: Main process exited, code=exited, status=127/n/a
Jan 22 22:27:43 raspberrypi systemd[1]: loxone_log.service: Unit entered failed state.
Jan 22 22:27:43 raspberrypi systemd[1]: loxone_log.service: Failed with result 'exit-code'.
Jan 22 22:27:43 raspberrypi systemd[1]: loxone_log.service: Service hold-off time over, scheduling restart.
Jan 22 22:27:43 raspberrypi systemd[1]: Stopped Loxone log.
-- Subject: Unit loxone_log.service has finished shutting down
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- Unit loxone_log.service has finished shutting down.
Jan 22 22:27:43 raspberrypi systemd[1]: loxone_log.service: Start request repeated too quickly.
Jan 22 22:27:43 raspberrypi systemd[1]: Failed to start Loxone log.
-- Subject: Unit loxone_log.service has failed
-- Defined-By: systemd
-- Support: https://www.debian.org/support
--
-- Unit loxone_log.service has failed.
--
-- The result is failed.
Jan 22 22:27:43 raspberrypi systemd[1]: loxone_log.service: Unit entered failed state.
Jan 22 22:27:43 raspberrypi systemd[1]: loxone_log.service: Failed with result 'exit-code'.
Jan 22 22:27:46 raspberrypi sudo[5507]: pi : TTY=pts/0 ; PWD=/home/pi ; USER=root ; COMMAND=/bin/journalctl -xe
Jan 22 22:27:46 raspberrypi sudo[5507]: pam_unix(sudo:session): session opened for user root by pi(uid=0)
lines 1206-1243/1243 (END)
Code: Select all
Jan 22 22:27:43 raspberrypi npm[5505]: /usr/bin/env: ‘node’: No such file or directory
Re: Auto running a program after boot
jdenver
"‘node’: No such file or directory" is telling you that it cannot fine the node executable. Which is no doubt called by "npm start".
Where is your node installed? You can find out with the "which" command. On my machine it works like so.
Normally in my systemd service files I do not use "npm start" but rather use node directly, specifying the full path to the executable I want to run:
Or, you could just install node.js globally in the standard place and then systemd would find it.
"‘node’: No such file or directory" is telling you that it cannot fine the node executable. Which is no doubt called by "npm start".
Where is your node installed? You can find out with the "which" command. On my machine it works like so.
That might seem like an odd location for it but I use NVM to install node.js and that is where it has put v11.1.0$ which node
/home/heater/.nvm/versions/node/v11.1.0/bin/node
Normally in my systemd service files I do not use "npm start" but rather use node directly, specifying the full path to the executable I want to run:
Alternatively you could set the node path into PATH using "ExecStartPre=" in the service file or perhaps using the "Environment=" option.ExecStart=/home/heater/.nvm/versions/node/v11.1.0/bin/node /home/pi/propanel/propanel.js
Or, you could just install node.js globally in the standard place and then systemd would find it.
Slava Ukrayini.