best97
Posts: 5
Joined: Tue Feb 25, 2014 2:36 pm

Run script at boot

Tue Feb 25, 2014 2:40 pm

Hi, I have a script file and I would like that that script was executed in every single boot, how can I do that?
I searched and nothing worked... I'm new in the rasp and linux, so I don't know much...
Thank you
:)

lorenz92
Posts: 10
Joined: Tue Feb 25, 2014 2:34 pm

Re: Run script at boot

Tue Feb 25, 2014 4:05 pm

There are actually several ways for doing that.
One way I used in a project of mine (which I prefer) is by using the init run-level based rc scripts.
Basically you will encounter the command "service" to start and stop system services i.e. your personal service.
Following this method you will also have the possibility of doing "service <your script> start" for example, which is neat and handy.
NOTE: the following steps usually require root permissions. So just issue every command with sudo or log into a root shell (sudo -s, beware that everything you do is done with root permissions!!)

Code: Select all

# update boot / reboot files
cp <your script> /etc/init.d/
# do it as soon as the device is going down,
#   both for shutdown and reboot
update-rc.d /etc/init.d/<your script> defaults
(If I couldn't have been clear enough, this has to be done after looking at the following part...)

The <your script> part is something like the following:

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          <your script name>
# Required-Start:    $all
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage my cool stuff
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions
# If you need to source some other scripts, do it here

case "$1" in
  start)
    log_begin_msg "Starting my super cool service"
# do something
    log_end_msg $?
    exit 0
    ;;
  stop)
    log_begin_msg "Stopping the coolest service ever unfortunately"

    # do something to kill the service or cleanup or nothing

    log_end_msg $?
    exit 0
    ;;
  *)
    echo "Usage: /etc/init.d/<your script> {start|stop}"
    exit 1
    ;;
esac
That's it :)
Another way (but hackish) I used to do on an embedded system (but because that was not a debian, so beware, it seems more simple but an update of the system might screw it up) is to modify the /etc/profile.

best97
Posts: 5
Joined: Tue Feb 25, 2014 2:36 pm

Re: Run script at boot

Tue Feb 25, 2014 8:15 pm

Ok, thank you :), just have a couple of questions.
When I run:

Code: Select all

cp <your script> /etc/init.d/
In the <you script>, should I put my script name or the path to my script? If it is the name, does it matter if it is in the pi folder?

And I didn't get what I have to do when you say:
"The <your script> part is something like the following:

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          <your script name>
# Required-Start:    $all
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage my cool stuff
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions
# If you need to source some other scripts, do it here

case "$1" in
  start)
    log_begin_msg "Starting my super cool service"
# do something
    log_end_msg $?
    exit 0
    ;;
  stop)
    log_begin_msg "Stopping the coolest service ever unfortunately"

    # do something to kill the service or cleanup or nothing

    log_end_msg $?
    exit 0
    ;;
  *)
    echo "Usage: /etc/init.d/<your script> {start|stop}"
    exit 1
    ;;
esac
"
Do I have to create another file with this?

My script is very simple:

Code: Select all

#!/bin/sh


sudo service hostapd stop

sudo service udhcpd stop

sudo service hostapd start

sudo service udhcpd start
Thank you so much for helping me :)

lorenz92
Posts: 10
Joined: Tue Feb 25, 2014 2:34 pm

Re: Run script at boot

Tue Feb 25, 2014 8:23 pm

Yes, <your script> is a path. If the script is in the current directory (i.e. the one at prompt) then actually you can just type its name.
Useless to say, avoid spaces ...
Sorry I was in a hurry and I did not really spent too much words :D
So, exactly, you have to create yet another script. Just copy and paste my template.
Then replace # do something with the content of your original script, to say:

Code: Select all

service hostapd stop
service udhcpd stop
service hostapd start
service udhcpd start
NOTE: why not sudo? Simple :) The script, if directly executed by the system is trivially executed as root. If issued by the user, the same here, it is implicit -> sudo service myservice...

Hope this can help, otherwise just poke here your doubts.

best97
Posts: 5
Joined: Tue Feb 25, 2014 2:36 pm

Re: Run script at boot

Tue Feb 25, 2014 9:09 pm

Thank you, so I just edited the template, is it ok like this? (my script is called "hotspot" and it is in the pi folder)

Code: Select all

#! /bin/sh
### BEGIN INIT INFO
# Provides:          </home/pi/hotspot>
# Required-Start:    $all
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Manage my cool stuff
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions
# If you need to source some other scripts, do it here

case "$1" in
  start)
    log_begin_msg "Starting my Hotspot service"
service hostapd stop
service udhcpd stop
service hostapd start
service udhcpd start
    log_end_msg $?
    exit 0
    ;;
  stop)
    log_begin_msg "Stopping the Hotspot service unfortunately"

    # do something to kill the service or cleanup or nothing

    log_end_msg $?
    exit 0
    ;;
  *)
    echo "Usage: /etc/init.d/home/pi/hotspot {start|stop}"
    exit 1
    ;;
esac
If everything is ok, what sould I do next? run the next 2 commands as sudo?

Code: Select all

cp <your script> /etc/init.d/
update-rc.d /etc/init.d/<your script> defaults
In these 2 commands <your script> is the path of the script that I just created from your template?

Thank you so much for helping me! :D :D :D :D :D :D

lorenz92
Posts: 10
Joined: Tue Feb 25, 2014 2:34 pm

Re: Run script at boot

Tue Feb 25, 2014 10:19 pm

Yes
and
Yes

sudo is usually required when writing / entering system directories (notably /etc tree).
Let me know!

EDIT: I noticed a minor problem (actually, I don't really know if it is a problem)
# Provides: </home/pi/hotspot>
it should be
# Provides: hotspot

best97
Posts: 5
Joined: Tue Feb 25, 2014 2:36 pm

Re: Run script at boot

Tue Feb 25, 2014 11:06 pm

I changed the #Provides and then ran the commands

Code: Select all

cp /home/pi/hotspotboot /etc/init.d/
update-rc.d /etc/init.d/home/pi/hotspotboot defaults
And it gives me an error:

Code: Select all

[email protected]:/home/pi# cp /home/pi/hotspotboot /etc/init.d/
[email protected]:/home/pi# update-rc.d /etc/init.d/home/pi/hotspotboot defaults
update-rc.d: using dependency based boot sequencing
update-rc.d: error: unable to read /etc/init.d//etc/init.d/home/pi/hotspotboot
What should I do?
Thank you :)

-----------------------------EDIT--------------------
I correct it, I did

Code: Select all

cp /home/pi/hotspotboot /etc/init.d/
cd /etc/init.d/
update-rc.d hotspotboot defaults
but now it gives me this error:

Code: Select all

[email protected]:/etc/init.d# update-rc.d hotspotboot defaultsupdate-rc.d: using dependency based boot sequencing
update-rc.d: warning: default start runlevel arguments (2 3 4 5) do not match hotspotboot Default-Start values (     2 3 4 5)
update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match hotspotboot Default-Stop values (      0 1 6)
insserv: script hotspotboot is not an executable regular file, skipped!
insserv: warning: script 'mathkernel' missing LSB tags and overrides
What should i do?

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

Re: Run script at boot

Wed Feb 26, 2014 5:09 am

The only line to really worry about is

Code: Select all

insserv: script hotspotboot is not an executable regular file, skipped!
This is because you haven't got the eXecute flag set on the file.

Try

Code: Select all

cd /etc/init.d
sudo chmod u+x hotspotboot
update-rc.d hotspotboot defaults
You will still get some warnings, but the error I mentioned above should have gone.
Last edited by rpdom on Wed Feb 26, 2014 7:37 am, edited 1 time in total.

lorenz92
Posts: 10
Joined: Tue Feb 25, 2014 2:34 pm

Re: Run script at boot

Wed Feb 26, 2014 7:35 am

Oh yes indeed, I forgot to tell you that...
The problem is for sure about executable bit ("+x").

best97
Posts: 5
Joined: Tue Feb 25, 2014 2:36 pm

Re: Run script at boot

Wed Feb 26, 2014 8:18 am

I tryied that and then this appeared, i belive it is an error:

Code: Select all

insserv: missing valid name for `Provides:' please add.
(just for you to know, My provides is like this: # Provides: hotspot)
What should I do?

User avatar
iinnovations
Posts: 621
Joined: Thu Jun 06, 2013 5:17 pm

Re: Run script at boot

Sat Mar 01, 2014 5:32 pm

It is much simpler to add your commands to the end of /etc/rc.local . Having a system service is nice, but an unnecessary level of complication for simple tasks.

Typically, I create a boot.sh script, invoke it from rc.local. Then I can keep boot.sh in a git repo or somewhere else handy.

As for the commands invoked here, you should probably just configure your dhcp and access point to not start on boot and turn them on when you want. I do this via a boot script as well, but using python. I have a python call read network configuration from a database and then act accordingly. Remember that you can invoke python scripts from rc.local as well.

Colin
CuPID Controls :: Open Source browser-based sensor and device control
interfaceinnovations.org/cupidcontrols.html
cupidcontrols.com

Biruntha
Posts: 9
Joined: Sun Oct 18, 2015 5:34 am
Location: Jaffna, Srilanka
Contact: Website

Re: Run script at boot

Sun Nov 22, 2015 1:00 am

what you mentioned <your script>. is it python script?
how can i change this steps to python script?
Last edited by Biruntha on Sun Nov 22, 2015 1:36 am, edited 1 time in total.

DirkS
Posts: 10130
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Run script at boot

Sun Nov 22, 2015 1:06 am

Biruntha wrote:what you mentioned <your script>. is it python script?
To which post are you referring?
Keep in mind that you're responding to a topic that's a year and a half old...

Biruntha
Posts: 9
Joined: Sun Oct 18, 2015 5:34 am
Location: Jaffna, Srilanka
Contact: Website

Re: Run script at boot

Sun Nov 22, 2015 1:38 am

DirkS wrote:
Biruntha wrote:what you mentioned <your script>. is it python script?
To which post are you referring?
Keep in mind that you're responding to a topic that's a year and a half old...
second post

fixermark
Posts: 2
Joined: Wed Jul 20, 2016 1:51 am

Re: Run script at boot

Wed Jul 20, 2016 1:55 am

Your script included the line

Code: Select all

# Provides:          </home/pi/hotspot>
The < and > characters (and possibly also the forward-slash?) aren't accepted in that context. Provides should be the name of the capability your init script is providing to the system, something more like "hwclock" or "alsa-utils". In your case,

Code: Select all

# Provides:          hotspot
... would probably be appropriate.

Return to “General discussion”