1#!/bin/sh
2# zzz - really simple suspend script.
3# Copied from void linux (licensed under public domain).
4
5# Fixed PATH, makes it a bit saner to allow execution of this via sudoers.
6export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
7
8USAGE="Usage: ${0##*/} [-nSzZR]
9 -n dry run (sleep for 5s instead of suspend/hibernate)
10 -S Low-power idle (ACPI S0)
11 -z suspend to RAM (ACPI S3) (default)
12 -Z hibernate to disk & power off (ACPI S4)
13 -R hibernate to disk & reboot"
14
15fail() { echo ${0##*/}: 1>&2 "$*"; exit 1; }
16
17export ZZZ_MODE=suspend
18export ZZZ_HIBERNATE_MODE=platform
19
20while getopts hnSzRZ: opt; do
21 case "$opt" in
22 n) ZZZ_MODE=noop ;;
23 S) ZZZ_MODE=standby ;;
24 z) ZZZ_MODE=suspend ;;
25 Z) ZZZ_MODE=hibernate ;;
26 R) ZZZ_MODE=hibernate; ZZZ_HIBERNATE_MODE=reboot ;;
27 *) fail "$USAGE" ;;
28 esac
29done
30shift $((OPTIND - 1))
31
32case "$ZZZ_MODE" in
33 suspend) grep -q mem /sys/power/state || fail "suspend not supported";;
34 hibernate) grep -q disk /sys/power/state || fail "hibernate not supported";;
35esac
36
37[ -w /sys/power/state ] || fail "sleep permission denied"
38
39(
40flock -n 9 || fail "another instance of zzz is running"
41
42case "$ZZZ_MODE" in
43 standby) printf freeze >/sys/power/state || fail "standby failed";;
44 suspend) printf mem >/sys/power/state || fail "suspend failed";;
45 hibernate)
46 echo $ZZZ_HIBERNATE_MODE >/sys/power/disk
47 printf disk >/sys/power/state || fail "hibernate failed";;
48 noop) sleep 5;;
49esac
50) 9</sys/power
51
52# vim: et:sw=2:sts=2