-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsystem-suspend
executable file
·64 lines (55 loc) · 1.55 KB
/
system-suspend
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/bin/sh -eu
###
### System variables
###
MY_NAME="$( basename "${0}" )"
MY_LOG_DIR="${HOME}/.log"
MY_LOG_FILE="${MY_LOG_DIR}/${MY_NAME}.err"
MY_DATE="$( date '+%Y-%m-%d' )"
MY_TIME="$( date '+%H:%M:%S' )"
################################################################################
##
## F U N C T I O N S
##
################################################################################
error() {
printf "%s\n" "${1}" >&2
printf "[%s] [%s] %s\n" "${MY_DATE}" "${MY_TIME}" "${1}" >> "${MY_LOG_FILE}"
}
################################################################################
##
## E N T R Y P O I N T
##
################################################################################
if [ ! -d "${MY_LOG_DIR}" ]; then
mkdir -p "${MY_LOG_DIR}"
fi
if [ ! -f "${MY_LOG_FILE}" ]; then
touch "${MY_LOG_FILE}"
fi
if [ ! -w "${MY_LOG_FILE}" ]; then
chmod u+w "${MY_LOG_FILE}"
fi
if command -v systemctl >/dev/null 2>&1; then
_ret=0
_err="$( systemctl suspend 2>&1)" || _ret=$?
if [ "${_ret}" != "0" ]; then
error "'systemctl suspend' did exit with non-zero: ${_ret}"
error "${_err}"
exit 1
fi
elif command -v dbus-send >/dev/null 2>&1; then
_ret=0
_err="$( dbus-send --system --print-reply \
--dest='org.freedesktop.login1' \
/org/freedesktop/#1 \
org.freedesktop.login1.Manager.Suspend boolean:true 2>&1 )" || _ret=$?
if [ "${_ret}" != "0" ]; then
error "'dbus-send' to suspend did exit with non-zero: ${_ret}"
error "${_err}"
exit 1
fi
else
error "You either need 'systemctl' or 'dbus-send'. None are available."
exit 1
fi