Retrogadgets
Posts: 12
Joined: Thu Aug 23, 2012 8:42 pm
Location: Sheffield, UK

Auto running a program after boot

Sun Mar 06, 2016 12:58 am

Loading the pi to the command line , what do I alter to make the pi run a program automatically

Can anyone help

Heater
Posts: 19435
Joined: Tue Jul 17, 2012 3:02 pm

Re: Auto running a program after boot

Sun Mar 06, 2016 5:47 am

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:

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
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:

Code: Select all

$ sudo cp   propanel.service  /etc/systemd/system/
$ sudo chmod u+rwx /etc/systemd/system/propanel.service
Now enable the service with a systemd command:

Code: Select all

$ sudo systemctl enable  propanel
And it will be run at boot time.

You can start it without rebooting with:

Code: Select all

sudo systemctl start   propanel
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:

Code: Select all

sudo systemctl stop   propanel
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.
Last edited by Heater on Sun Jan 08, 2017 10:26 am, edited 1 time in total.
Slava Ukrayini.

User avatar
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

Sun Mar 06, 2016 8:48 am

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.
Thanks for posting that clear and concise way to get systemd services running. It's a bunch easier than hacking /etc/init.d/skeleton.
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.

Retrogadgets
Posts: 12
Joined: Thu Aug 23, 2012 8:42 pm
Location: Sheffield, UK

Re: Auto running a program after boot

Sun Mar 06, 2016 10:01 am

Thanks , I'll give it a try on my pi 2 as a minecraft server

Retrogadgets
Posts: 12
Joined: Thu Aug 23, 2012 8:42 pm
Location: Sheffield, UK

Re: Auto running a program after boot

Sun Mar 06, 2016 6:50 pm

How do you pass parameters within this script say for example sudo java -Xmx512 -Xms990 -jarfile spigot.jar nogui

Thanks for your help

MarkR
Posts: 156
Joined: Fri Jan 25, 2013 1:55 pm

Re: Auto running a program after boot

Sun Mar 06, 2016 6:55 pm

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".

Heater
Posts: 19435
Joined: Tue Jul 17, 2012 3:02 pm

Re: Auto running a program after boot

Sun Mar 06, 2016 7:44 pm

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:

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
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.
Slava Ukrayini.

pmva
Posts: 1
Joined: Fri Apr 29, 2016 11:11 am

Re: Auto running a program after boot

Fri Apr 29, 2016 11:53 am

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. :oops:

Thank you in advance.

WildWalker
Posts: 43
Joined: Sat Mar 03, 2012 3:49 pm

Re: Auto running a program after boot

Tue Sep 13, 2016 1:14 pm

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:

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
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:

Code: Select all

$ sudo cp   propanel.service  /etc/systemd/system/
$ sudo chmod u+rwx /etc/systemd/system/propanel.service
Now enable the service with a systemd command:

Code: Select all

$ sudo systemctl enable  propanel
And it will be run at boot time.

You can start it without rebooting with:

Code: Select all

sudo systemctl start   propanel
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:

Code: Select all

sudo systemctl stop   propanel
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.
Nothing in here, but thank you.

klintkrossa
Posts: 84
Joined: Tue Nov 10, 2015 3:06 pm

Re: Auto running a program after boot

Fri Dec 09, 2016 6:41 am

Thanks
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.

Heater
Posts: 19435
Joined: Tue Jul 17, 2012 3:02 pm

Re: Auto running a program after boot

Fri Dec 09, 2016 9:44 am

How about just creating another service file for your other program.
Slava Ukrayini.

willbobby
Posts: 43
Joined: Sun Nov 06, 2016 2:40 pm

Re: Auto running a program after boot

Sun Sep 24, 2017 3:35 pm

Heater wrote:
Sun Mar 06, 2016 5:47 am
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:

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
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:

Code: Select all

$ sudo cp   propanel.service  /etc/systemd/system/
$ sudo chmod u+rwx /etc/systemd/system/propanel.service
Now enable the service with a systemd command:

Code: Select all

$ sudo systemctl enable  propanel
And it will be run at boot time.

You can start it without rebooting with:

Code: Select all

sudo systemctl start   propanel
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:

Code: Select all

sudo systemctl stop   propanel
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.


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.

User avatar
davidcoton
Posts: 7005
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Auto running a program after boot

Sun Sep 24, 2017 3:54 pm

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.
Q1, yes:
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
Q2, you obviously need to adapt the file, for example " ExecStart=python3 /home/pi/pythonprogs/prog.py"
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

willbobby
Posts: 43
Joined: Sun Nov 06, 2016 2:40 pm

Re: Auto running a program after boot

Sat Oct 28, 2017 1:45 pm

I have done everything as told but for some reason the code doesn't run after the boot up.

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
I know for definite that the code runs perfectly.

User avatar
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

Sat Oct 28, 2017 7:12 pm

Lose this line:

Code: Select all

WorkingDirectory=/home/pi/Desktop/Model_Phalanx/Adafruit_PWM_Servo/Servo_Keyboard
Lose this line:

Code: Select all

Environment=NODE_ENV=production
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.
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.

Sdack
Posts: 6
Joined: Tue Nov 07, 2017 6:54 am

Re: Auto running a program after boot

Fri Nov 17, 2017 3:59 am

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

User avatar
rpdom
Posts: 21842
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Auto running a program after boot

Fri Nov 17, 2017 4:58 am

If you've got wiringpi installed (it usually is), you could add the following into your bash script

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
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.

Sdack
Posts: 6
Joined: Tue Nov 07, 2017 6:54 am

Re: Auto running a program after boot

Fri Nov 17, 2017 5:12 am

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

rohank752
Posts: 1
Joined: Thu Dec 14, 2017 2:05 pm

Re: Auto running a program after boot

Thu Dec 14, 2017 2:11 pm

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

OldPCGuy
Posts: 86
Joined: Mon Jan 02, 2017 4:53 pm
Location: Detroit Michigan

Re: Auto running a program after boot

Sat Apr 28, 2018 4:24 pm

Stupid question...

... to disable running the file after boot do you just delete the *.service file? No other housekeeping required ?

User avatar
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

Sun Apr 29, 2018 1:27 am

systemctl disable xyzzy.service

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.

grininmonkey
Posts: 145
Joined: Mon Jul 30, 2018 3:44 pm

Re: Auto running a program after boot

Mon Jul 30, 2018 9:14 pm

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?

grininmonkey
Posts: 145
Joined: Mon Jul 30, 2018 3:44 pm

Re: Auto running a program after boot

Thu Aug 02, 2018 1:11 pm

grininmonkey wrote:
Mon Jul 30, 2018 9:14 pm
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?
I figured it out... sharing just in case someone else stumbles across this thread.

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

jdenver
Posts: 15
Joined: Wed Sep 21, 2016 4:35 pm

Re: Auto running a program after boot

Tue Jan 22, 2019 10:36 pm

Still struggling with this, the new code is:

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
Permissions set to 755

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)
It seems to be a problem with

Code: Select all

Jan 22 22:27:43 raspberrypi npm[5505]: /usr/bin/env: ‘node’: No such file or directory
from the above I think, but can't work out how to solve it.

Heater
Posts: 19435
Joined: Tue Jul 17, 2012 3:02 pm

Re: Auto running a program after boot

Wed Jan 23, 2019 6:27 pm

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.
$ which node
/home/heater/.nvm/versions/node/v11.1.0/bin/node
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

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:
ExecStart=/home/heater/.nvm/versions/node/v11.1.0/bin/node /home/pi/propanel/propanel.js
Alternatively you could set the node path into PATH using "ExecStartPre=" in the service file or perhaps using the "Environment=" option.

Or, you could just install node.js globally in the standard place and then systemd would find it.
Slava Ukrayini.

Return to “General discussion”