Skip to content

Instantly share code, notes, and snippets.

@spthm
Last active June 22, 2018 20:46
Show Gist options
  • Save spthm/7f3c3a7b96c45fce3a49022cbd1cf9d9 to your computer and use it in GitHub Desktop.
Save spthm/7f3c3a7b96c45fce3a49022cbd1cf9d9 to your computer and use it in GitHub Desktop.
Turn on/off Raspberry Pi 2 Model B POWER LED
#!/bin/bash
if [[ $# -eq 0 || "$1" == "-h" || "$1" == "--help" ]]; then
echo "Usage: led_ctl [on|off|auto]"
echo "on -- turn POWER LED on"
echo "off -- turn POWER LED off"
echo "auto -- turn POWER LED on if between 08:00 and 20:00, else turn off"
echo "Run as root."
exit 0
fi
if [[ $EUID -ne 0 ]]; then
echo "ERROR: Must run as root"
exit 1
fi
function turn_pwr_led_off {
echo 0 > /sys/class/leds/led1/brightness
}
function turn_pwr_led_on {
echo 1 > /sys/class/leds/led1/brightness
}
if [[ "$1" == "off" ]]; then
turn_pwr_led_off
fi
if [[ "$1" == "on" ]]; then
turn_pwr_led_on
fi
if [[ "$1" == "auto" ]]; then
now=$(date +"%H%M")
if [[ 800 -le $now && 2000 -ge $now ]]; then
turn_pwr_led_on
else
turn_pwr_led_off
fi
fi
@spthm
Copy link
Author

spthm commented Jun 22, 2018

Put in /usr/local/bin (or anywhere that is on your PATH), and make sure it has the right owner and permissions,

$ sudo chown root:staff /usr/local/bin/led_ctl
$ sudo chmod 755 /usr/local/bin/led_ctl

The staff group is a Debian/Raspbian/Ubuntu/etc. thing; you probably want root:root if you're on a distro which isn't a Debian derivative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment