GPIO python script not calling bash with subprocess via cron

129 views Asked by At

I have a Python script calling GPIOZERO to watch for button presses, calling a few different functions (turn off LCD etc) these work from cron, but the calls to bash from subprocess do not. Runs fine from command line. The other functions in this script do work, but not this one, I have double checked absolute paths and they appear to be correct.

in myscript.py:

#!/usr/local/bin/python
sys.path.append('/home/pi/.local/lib/python2.7/site-packages')
import subprocess

#set global batch mode on or off
def running():
    global r
    r = not r
    if r is True:
         subprocess.Popen(['/home/pi/Documents/ytu/desktop_col.sh', 'run']) 
     print "run mode"

elif r is False:
     subprocess.Popen(['/home/pi/Documents/ytu/desktop_col.sh' ,'stop'])
     print "pause mode"

When run from python command line, script works fine, but not from Cron or any other startup method. This is the bash code it calls. I am switching desktop backgrounds to use as an indicator of status.

    #!/usr/bin/bash
    alert_display=`cat /media/pi/VDRIVE/prefs/alert_display_number.txt`

   export XAUTHORITY=/home/pi/.Xauthority
   export DISPLAY=:$alert_display


   if [ "$1" = "run" ] ; then
     pcmanfm --set-wallpaper="/home/pi/Downloads/youtube-512.png"
     echo "run" > "/media/pi/VDRIVE/prefs/run-status.txt"
   elif [ "$1" = "stop" ] ; then
     pcmanfm --wallpaper-mode=color
     echo "stop" > "/media/pi/VDRIVE/prefs/run-status.txt"

   fi
1

There are 1 answers

0
Nowski On

Turns out it was the pcmanfm call that was the culprit. Was missing the line:

    export XDG_RUNTIME_DIR=/run/user/1000

Which meant the code was not executing in the right display space. and either throwing an error, or telling me that "Desktop Manager is not active"

found the solution at: Pcmanfm set wallpaper fails on Raspbian stretch in cron

Hope this helps someone