1

I have a python script that I would like to be run at startup. I have made a init.d script as follows:

#! /bin/sh
case "$1" in 

  start)
    echo "starting pitracker"
    su 'pi' -c 'python3 /home/pi/python/main/myprogram.py&'
  ;;

  stop)
    echo "stopping"  
  ;;

esac
exit 0

I have changed the permission of this startup script to 755. I have also created a symbolic link using

update-rc.d /etc/init.d/dnscheck defaults

The problem is when I use the 'pi' user in my startup script, I get the following error message:

File "/home/pi/python/main/myprogram.py" , line 21, in <module> 
import RPi.GPIO as GOPI
RunTimeError: No access to /dev/mem. Try running as root!

And when I change the username in my startup script to 'root', nothing happens at all. I believe this is a permission problem for accessing the GPIO. How can I start up my script without running into this issue?

3 Answers 3

2

There is no need for the su as the init.d scripts run as root. In general any command in init should have a full path to the executable.

Incidentally it would probably be easier to edit /etc/rc.local

1
  • I tried /etc/rc.local and nothing happens. The script is not even executed.
    – Essi Shams
    Commented Feb 4, 2014 at 14:17
1

Okay, I'm just starting down the init.d path myself, so don't consider this an authoritative answer. But I think you need to include an LSB Header along these lines (your mileage may vary):

#! /bin/sh 
#
### BEGIN INIT INFO
# Provides:          pitracker
# Required-Start:    $all
# Required-Stop:     
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Track pi.
# Description:       This service is used to track a pi.
### END INIT INFO

The Required-Start: and Required-Stop: lines make sure that certain system resources are up and running before it tries to start your program. I think -- but I'm not sure -- that the message you're seeing is because the system hasn't yet set up /dev/mem when it tries to launch your app.

Perhaps give it a shot?

0

Set the HOME and USER environment variables with HOME=/home/pi and USER=pi at the start of the script and add an LSB header as described in the previous answer. The main problem is that the python script hasn't got root privileges to use the GPIO pins. To overcome, replace su 'pi' -c 'python3 /home/pi/python/main/myprogram.py&' with su 'root' -c 'python3 /home/pi/python/main/myprogram.py&'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.