0

Using the directions on this webpage, I downloaded raspi_gpio_actions.sh.

I then created a script directory, "scripts" in /home/pi and placed the shell script there. I ran these commands: sudo chmod 755 raspi_gpio_actions.sh and sudo ./raspi_gpio_actions.sh.

I then added this line to /etc/crontab:

@reboot        root    /home/user/scripts/raspi_gpio_actions.sh

saved and closed the file, and rebooted my Raspberry Pi 2. I placed a jumper across the pins referenced in the shell script as 5 and 6 (and pin 3 and the ground pin above in the diagram below). During earlier edits of this post, I was not able to get it to shutdown, but now I can at times.

The script only sometimes shuts down the Raspberry Pi, when I run the script manually as root. It only does this when the jumper is plugged into both pins before the program runs, and it will shutdown. But if I plug in the jumper across the pins after the program is running, it will not shutdown.

This is the shell script (which has not changed since 2013, as of the time of the post.)

#! /bin/bash

 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  
#                                                                             #     
# This script configures which GPIO pins of the Raspberry Pi will be          #
# available and in which direction (read or write). It also takes some        #
# actions if someone interacts with the selected pins.                        #
#                                                                             #
# More info about the GPIO: http://elinux.org/RPi_Low-level_peripherals       #
#                                                                             #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
#                                                                             #
# -Raspberry Pi Rev2. GPIO layout:                                            #
#                                                                             #
#           2  4  6  8  10 12 14 16 18 20 22 24 26    _R_C_A_                 #
#           |  |  |  |  |  |  |  |  |  |  |  |  |     |______|                #
#           --------------------------------------                            #
#           |  |  |  |  |  |  |  |  |  |  |  |  |                             #
#    _      1  3  5  7  9  11 13 15 17 19 21 23 25                            #
#   | |                                                                       #
#   | | SD Card                                                               #
#   | |                                                                       #
#   |_|                                                                       #
#                                                                             #
#   P1-01: 3.3v       P1-10: GPIO15     P1-20: GND                            #
#   P1-02: 5.0v       P1-11: GPIO17     P1-21: GPIO9                          #
#   P1-03: GPIO2      P1-12: GPIO18     P1-22: GPIO25                         #
#   P1-04: 5.0v       P1-13: GPIO27     P1-23: GPIO11                         #
#   P1-05: GPIO3      P1-14: GND        P1-24: GPIO8                          #
#   P1-06: GND        P1-15: GPIO22     P1-25: GND                            #
#   P1-07: GPIO4      P1-16: GPIO23     P1-26: GPIO7                          #
#   P1-08: GPIO14     P1-17: 3.3v                                             #
#   P1-09: GND        P1-18: GPIO24                                           #
#                     P1-19: GPIO10                                           #
#                                                                             #
# NOTE: Pins GPIO2 and GPIO3 have a 1K8 pull up resistor, which means that    # 
# you can short cirtuit them directly to ground and you'll get a 1 as a non   #
# connected value (iddle) and a 0 as a connected value (short circuit to GND) #
# when reading them.                                                          #
#                                                                             #
#                                                                             #
#  by g0to                                                                    #
#                                                                             #
 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #  


if [[ $EUID -ne 0 ]]; then
   echo "Error: This script must be run as root" >&2
   exit 1
fi

SHUTDOWN_PIN="3"
#DHCP_ON_PIN="2"

echo "$SHUTDOWN_PIN" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio"$SHUTDOWN_PIN"/direction
#echo "$DHCP_ON_PIN" > /sys/class/gpio/export
#echo "in" > /sys/class/gpio/gpio"$DHCP_ON_PIN"/direction

while ( true )
do
    # check if the pin is connected to GND and, if so, halt the system
    if [ $(</sys/class/gpio/gpio"$SHUTDOWN_PIN"/value) == 0 ]
    then
        echo "$SHUTDOWN_PIN" > /sys/class/gpio/unexport
        shutdown -h now "System halted by a GPIO action"
#   elif [ $(</sys/class/gpio/gpio"$DHCP_ON_PIN"/value) == 0 ]
#   then
#       dhclient eth0
    fi

    sleep 60
done

enter image description here

enter image description here

  • Looks like part of my problem is I am not waiting long enough! – user3273814 Dec 30 '16 at 6:05
  • My real problem was I forgot to add a newline to the end of the last line! So now it works. – user3273814 Dec 30 '16 at 6:26
0

The solution to this problem is to check and debug crontab.

First run the command:

ps -ef | grep cron | grep -v grep

If it returns something like this

root       480     1  0 23:06 ?        00:00:00 /usr/sbin/cron -f

You are good to go. Otherwise restart.

/sbin/service cron start

Also check to make sure last line has a newline!

Source: https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it

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.