1

I have defined a bash script to prepare gpio27 for setting it to 0 or 1 through additional scripts on a RaspberryPi Zero 2 with Buster installed.

The script is the following one:

#!/bin/bash
echo "27" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio27/direction

1- If I run this script as user "pi", I get a permission denied error (NOK):

/home/pi/bin/prep27: line 3: /sys/class/gpio/gpio27/direction: Permision denied

2- If I run the conflictive line ‘echo "out" > /sys/class/gpio/gpio27/direction’ as user pi (no sudo), I get no error (OK):

echo "out" > /sys/class/gpio/gpio27/direction

3- if I replace in the script the third line by ‘sudo echo "out" > /sys/class/gpio/gpio27/direction’ and I execute the script as pi, I also get a permission denied error (NOK):

#!/bin/bash
echo "27" > /sys/class/gpio/export
sudo echo "out" > /sys/class/gpio/gpio27/direction

4- if i sudo execute the script as user pi, I get no error (OK)

sudo /home/pi/bin/prep27

Could you help me understand these permission issues with the script and its contents ?

Thanks very much

jivaro
  • 41
  • 4

1 Answers1

0

I believe the problem you're facing has to do with the way the Raspi is allocating permissions after you've created the pin. That works using a mechanism called udev that changes the permissions on the new "file" /sys/class/gpio/gpio27/direction after it is created by the previous export line. The problem now is, that this requires a bit of time to work correctly.

To work around this, you have to add a delay after the echo "27" > /sys/class/gpio/export line (1 second or so should do). Alternatively, you can repeat the direction line until it works.

PMF
  • 14,535
  • 3
  • 23
  • 49