Skip to content

Instantly share code, notes, and snippets.

@freayd
Created May 12, 2013 17:26
Show Gist options
  • Save freayd/5564314 to your computer and use it in GitHub Desktop.
Save freayd/5564314 to your computer and use it in GitHub Desktop.
Prevent brightness (both for screen and keyboard backlight) to be reset to maximum on every restart (Ubuntu on a MacBook only)

Install

  1. Copy the script to the file /usr/local/bin/save-and-restore-brightness.sh
  2. Run the following command: sudo chmod a+x /usr/local/bin/save-and-restore-brightness.sh
  3. Append the following line to the file /etc/pam.d/lightdm: session optional pam_exec.so /usr/local/bin/save-and-restore-brightness.sh

Note

This script can be adapted to any laptop by adjusting the variables SCREEN_BRIGHTNESS_FILE and KEYBOARD_BRIGHTNESS_FILE.

#!/bin/bash
SCREEN_BRIGHTNESS_FILE="/sys/class/backlight/acpi_video0/brightness"
KEYBOARD_BRIGHTNESS_FILE="/sys/class/leds/smc::kbd_backlight/brightness"
eval PAM_USER_HOME="~$PAM_USER"
CONFIG_FILE="$PAM_USER_HOME/.brightness.cfg"
case "$PAM_TYPE" in
close_session)
# Save
su "$PAM_USER" -c "touch '$CONFIG_FILE'"
echo 'SCREEN_BRIGHTNESS='$( cat "$SCREEN_BRIGHTNESS_FILE" ) > "$CONFIG_FILE"
echo 'KEYBOARD_BRIGHTNESS='$( cat "$KEYBOARD_BRIGHTNESS_FILE" ) >> "$CONFIG_FILE"
;;
open_session)
# Restore
if [ -f "$CONFIG_FILE" ]; then
source "$CONFIG_FILE"
if [ -n "$SCREEN_BRIGHTNESS" ]; then
echo "$SCREEN_BRIGHTNESS" > "$SCREEN_BRIGHTNESS_FILE"
fi
if [ -n "$KEYBOARD_BRIGHTNESS" ]; then
echo "$KEYBOARD_BRIGHTNESS" > "$KEYBOARD_BRIGHTNESS_FILE"
fi
fi
;;
*)
if [ -z "$PAM_TYPE" ]; then
echo "ERROR: Do not call this script directly on a shell. The script is designed to run automatically (with pam_exec)."
fi
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment