Skip to content

Instantly share code, notes, and snippets.

@tgrushka
Last active August 7, 2024 20:44
Show Gist options
  • Save tgrushka/6cb4807ed5c66ed2c5f7dc9a1734f437 to your computer and use it in GitHub Desktop.
Save tgrushka/6cb4807ed5c66ed2c5f7dc9a1734f437 to your computer and use it in GitHub Desktop.
Linux Gnome caffeinate shell script - Keep system awake / prevent sleep, like caffeinate on macOS
#!/usr/bin/bash
# caffeinate: Keep system awake / prevent sleep, like caffeinate on macOS.
# for Gnome desktop
# USAGE:
# Place this script somewhere in $PATH and set executable.
# Run `caffeinate` from terminal.
# Hit Ctrl-C to stop.
# To timeout, run `timeout SECONDS caffeinate`, where SECONDS is # of seconds.
START=`date +%s`
POWER_SCHEMA=org.gnome.settings-daemon.plugins.power
SLEEP_AC_KEY=sleep-inactive-ac-type
SLEEP_BATTERY_KEY=sleep-inactive-battery-type
SAVED_AC=$(gsettings get $POWER_SCHEMA $SLEEP_AC_KEY 2>/dev/null)
SAVED_BATTERY=$(gsettings get $POWER_SCHEMA $SLEEP_BATTERY_KEY 2>/dev/null)
gsettings set $POWER_SCHEMA $SLEEP_AC_KEY 'nothing' 2>/dev/null
gsettings set $POWER_SCHEMA $SLEEP_BATTERY_KEY 'nothing' 2>/dev/null
trap revert EXIT
revert() {
gsettings set $POWER_SCHEMA $SLEEP_AC_KEY $SAVED_AC 2>/dev/null
gsettings set $POWER_SCHEMA $SLEEP_BATTERY_KEY $SAVED_BATTERY 2>/dev/null
END=`date +%s`
RUNTIME=$((END-START))
echo ""
echo "Caffeinated for $RUNTIME seconds"
exit
}
read -r -d '' _ </dev/tty
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment