douglaseucken
Posts: 2
Joined: Fri Oct 19, 2012 8:00 pm

init.d Script Question

Fri Oct 19, 2012 8:07 pm

I have written a basic bash script for some GPIO functions that I would like to run automatically upon start up and in the background continuously.

I am looking at adding an init.d script and have read through the skeleton file that already exists at /etc/init.d/.

My question is do you actually put the GPIO scripting in the init.d script file, or do you reference the GPIO script to be run from inside the init.d script? Or asking another way, do I put the contents of my GPIO script in the new init.d script?

Imager
Posts: 2
Joined: Sat Oct 20, 2012 8:38 am

Re: init.d Script Question

Sat Oct 20, 2012 8:52 am

Hallo,

last week I made an interface for my RPi to shut down on an bat -pack on AC-cut.
I hope it helps you to.

Marnix



How to add a script at startup?

Example : begin

A/ Create the scrip to be executed at start, here begin2.py


#!/usr/bin/python

# begin2.py 10/2012 Marnix Deforche

import RPi.GPIO as io
import time, sys, os

io.setmode(io.BOARD) # to use Raspberry Pi board pin numbers
io.setup(16, io.IN, pull_up_down=io.PUD_UP) # power on

if not io.input(16): # power on = 0 !
# bat.-supply is connected
io.setup(18, io.OUT) # RPi in run
io.output(18, True)

print("")
print("")
print(" ***************** Begin ******************")
print(" * Set up GPIO 23 (P1-16) : pull-up input *")
print(" * Set up GPIO 24 (P1-18) : output = 1 *")
print(" * Auto shutdown is acivated *")
print(" ******************************************")
print("")

while True:
time.sleep(1) # once a seconde
if io.input(16): # power on = 0 !
os.system("sudo shutdown -h now")
else:
# bat.-supply is NOT connected
print("")
print("[info] No auto shutdown")
io.cleanup()
sys.exit(0)




Once testing is finished :

1/make the script executable.

2/Make a copy in a secure place, here /usr/local/bin/
pi@raspberrypi ~ $sudo cp begin2.py /usr/local/bin/

3/Rename it if you want
pi@raspberrypi ~ $ sudo mv /usr/local/bin/begin2.py /usr/local/bin/begin




B/ Create the startup-script

Looke at :
/etc/init.d/README

You can start with:
/etc/init.d/skeleton or a copy of an existing one .

root@raspberrypi:/home/pi# leafpad /etc/init.d/begin
Pay attention :
use absolute path if not in systeem-path
by a repeating scrip , start in background &

#!/bin/sh

### BEGIN INIT INFO
# Provides: begin
# Required-Start:
# Required-Stop:
# Should-Stop:
# X-Start-Before: checkroot
# Default-Start: S
# Default-Stop:
# Short-Description: setup for auto-shutdow
# Description:
### END INIT INFO

set -e

case "${1:-}" in

start)
/usr/local/bin/begin& ;;

*)
echo "Usage: ${0:-} {start}" >&2
exit 1
;;
esac





C/ Add the startup-scrip to the startup-list

Look at: man update-rc.d

Try out with -n :
root@raspberrypi:/home/pi# update-rc.d -n begin defaults 01

For real :
root@raspberrypi:/home/pi# update-rc.d begin defaults 01

Imager
Posts: 2
Joined: Sat Oct 20, 2012 8:38 am

Re: init.d Script Question

Sat Oct 20, 2012 11:34 am

Hallo,

last week I made an interface for my RPi to shut down on an bat -pack on AC-cut.
I hope it helps you to.

Marnix


How to add a script at startup?

Example : begin

A/ Create the scrip to be executed at start, here begin2.py

Code: Select all

#!/usr/bin/python

# begin2.py  10/2012     Marnix Deforche

import RPi.GPIO as io
import time, sys, os

io.setmode(io.BOARD)    # to use Raspberry Pi board pin numbers
io.setup(16, io.IN, pull_up_down=io.PUD_UP)     # power on

if not io.input(16):        # power on = 0 !
    # bat.-supply is connected
    io.setup(18, io.OUT)    # RPi in run
    io.output(18, True)

    print("")
    print("")
    print("     ***************** Begin ******************")
    print("     * Set up GPIO 23 (P1-16) : pull-up input *")
    print("     * Set up GPIO 24 (P1-18) : output = 1    *")
    print("     *      Auto shutdown is acivated         *")
    print("     ******************************************")
    print("")

    while True:
        time.sleep(1)           # once a  seconde
        if io.input(16):        # power on = 0 !
            os.system("sudo shutdown -h now")
else:
    # bat.-supply is NOT connected
    print("")
    print("[info] No auto shutdown")
    io.cleanup()
    sys.exit(0)


Once testing is finished :

1/make the script executable.

2/Make a copy in a secure place, here /usr/local/bin/
pi@raspberrypi ~ $sudo cp begin2.py /usr/local/bin/

3/Rename it if you want
pi@raspberrypi ~ $ sudo mv /usr/local/bin/begin2.py /usr/local/bin/begin




B/ Create the startup-script

Look at :
/etc/init.d/README

You can start with:
/etc/init.d/skeleton or a copy of an existing one .

root@raspberrypi:/home/pi# leafpad /etc/init.d/begin
Pay attention :
use absolute path if not in systeem-path
by a repeating scrip , start in background &

Code: Select all

#!/bin/sh

### BEGIN INIT INFO
# Provides:          begin
# Required-Start:
# Required-Stop:
# Should-Stop:       
# X-Start-Before:    checkroot
# Default-Start:     S
# Default-Stop:      
# Short-Description: setup for auto-shutdow
# Description:       
### END INIT INFO

set -e

case "${1:-}" in

  start)
 	/usr/local/bin/begin& ;;

  *)
        echo "Usage: ${0:-} {start}" >&2
        exit 1
        ;;
esac
C/ Add the startup-scrip to the startup-list

Look at: man update-rc.d

Try out with -n :
root@raspberrypi:/home/pi# update-rc.d -n begin defaults 01

For real :
root@raspberrypi:/home/pi# update-rc.d begin defaults 01

douglaseucken
Posts: 2
Joined: Fri Oct 19, 2012 8:00 pm

Re: init.d Script Question

Mon Oct 22, 2012 5:53 pm

Thanks for the reply. It turned out I was following all the right steps initially, but I had installed the Webiopi program earlier and it was also running at boot. My program started correctly, but then the Webiopi started and over wrote what my program had done with the GPIO pins.

Once I removed Webiopi from the update-rc.d list everything was working as expected.

Thanks again though.

Return to “General programming discussion”