Python script not working when launched at startup of RPi

565 Views Asked by At

RPi 1B with V1 cam. Python script takes a picture when pushbutton hooked to gpio is pressed. Then picture is emailed via Mutt. All working fine when doing step by step. But not doing as intended when lauched automatically at startup.

import subprocess
from datetime import datetime
from gpiozero import Button

button = Button(17)


while True:
        button.wait_for_press()
        time = datetime.now()
        filename = "capture-%04d%02d%02d-%02d%02d%02d.jpg" % (time.year, time.month, time.day, time.hour, time.minute, time.second)
        subprocess.call("raspistill -t 500 -o %s" % filename, shell=True)
        subprocess.call("echo "" | mutt -s 'Someone at the door' -i messageBody.txt [email protected] -a %s" % filename, shell=True)

All working fine when typing :

$ python raspicam.py

I get a nice email within seconds with picture attached to it.

Next logical step is to get this script to be launched at startup:

$ nano launcher.sh
#!/bin/sh
# launcher.sh
cd /
cd home/pi
python doorbell02.py
cd /

$ chmod 755 launcher.sh

$ sh launcher.sh

Then get it to be launched at startup via cron :

$ mkdir logs

$ sudo crontab -e

add: @reboot sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1

At next reboot all working fine except sending mail via with mutt.

$ ps aux shows that my python script and script launcher belongs to "root"... is it where trouble comes from ?

root       475  0.0  0.0      0     0 ?        S    16:51   0:00 [cifsd]
root       500  0.0  0.6   7932  2300 ?        Ss   16:51   0:00 /usr/sbin/cron -f
root       502  0.0  0.6   9452  2384 ?        S    16:51   0:00 /usr/sbin/CRON -f
root       506  0.0  0.3   1924  1148 ?        Ss   16:51   0:00 /bin/sh -c sh /home/pi/launcher.sh >/home/pi/logs/cronlog 2>&1
root       511  0.0  0.2   1924  1108 ?        S    16:51   0:00 sh /home/pi/launcher.sh
root       513  1.5  2.5  34348  9728 ?        Sl   16:51   4:25 python doorbell02.py

I am also unable to get pdb to work alongside with my script to get some log or debug info... Some hints would be very appreciated Thank you very much for your time

1

There are 1 best solutions below

0
Koxo On

Try using absolute paths in your code. It helped me in my case.