1

I have this python script that is intended to run from startup and listen for a buttonpress. If there is a buttonpress (held for 2s) the gpio script should run two bash scripts. The python script runs if it is done with python /path/to/script/script.py so I know it is working. If I try to run in from startup by a crontab -e @reboot /path/to/script/script.py I can see that it is active when running the top command. However, the button press will not fire the intended bash scripts. I have tried and modify ~/.bashrc and tried to have a bash script in crontab -e with sudo python /path/to/script.py. None of them worked. To complicate things further, If I change it to call a command instead, like killall -9 openvpn for example... then it works.. what am I doing wrong?

The python script looks like the following

from gpiozero import Button
from signal import pause
import os
def runboth():
     os.system("/opt/script/script_a.sh")

     os.system("/opt/script/script_b.sh")


runboth_btn = Button(23, hold_time=2)
runboth_btn.when_held = runboth


pause()

`

Below is one bash script I try to run with the button press. Its located in ~/opt/script# and has the permissions -rwxr-xr-x 1 root root 240 Mar 21 11:58 match_email.sh

#!/bin/bash
#run match
python /opt/tmp_email/email_match.py

wait
mv /opt/tmp_email/output.csv /opt/emailoutput$(date -d "today"+"%Y%m%d%H%M").csv

If I try to run it with a subprocess.call looking like

from gpiozero import Button
from signal import pause
import subprocess
def runboth():
    subprocess.call(['sh', '/opt/script/script_a.sh'], shell=False)

    subprocess.call(['sh', '/opt/script/script_b.sh'], shell=False)

runboth_btn = Button(17, hold_time=2)
runboth_btn.when_held = runboth


pause()

But didn´t work either..

Crontab looks like

@reboot /opt/script/run_both.py

I have had a look at Run a shell script from a python script when a button is pressed but that didn´t help me.. Any help is much appreciated!

  • Could you please show us at least one of the scripts? Along with the location of the scripts. – NomadMaker Mar 21 '18 at 2:10
  • 1
    try running the scripts with this instead and see if it works: import subprocess then call them with subprocess.call(['sh', '/home/pi/Desktop/script/script_a.sh'], shell=False) – scitronboy Mar 21 '18 at 2:58
  • Edited post with bash example and subprocess .. didn´t work unfortunatly.. – Noah Smith Mar 21 '18 at 15:40
1

I can see a few things that might have gone wrong.

  1. You are defining the location of the scripts with the "~/" notation, which only works if the same user that is running the python script is storing the scripts. I'd put the scripts in something like /opt/project_name/script_a.sh

  2. The scripts might not be executable by the user that is running the python script.

  3. You might not have the #!/bin/bash at the top of the shell scripts.

1

The answer is here. As mentioned in this post, systemd is the most reliable. I had to laborate with what user I ran it with but otherwise quite straightforward. https://www.raspberrypi.org/forums/viewtopic.php?t=200174

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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