2008-11-11 14:36:30 +00:00
|
|
|
#!/bin/sh
|
|
|
|
|
2015-05-19 10:08:06 +00:00
|
|
|
PATH=/usr/sbin:/usr/bin
|
2008-11-11 14:36:30 +00:00
|
|
|
|
2017-01-18 15:36:30 +00:00
|
|
|
# $1 = session number
|
|
|
|
function get_session_processes() {
|
|
|
|
local uid=$(loginctl show-session $1 | grep '^User=' | sed -r -e 's/^User=(.*)$/\1/')
|
|
|
|
systemd-cgls "/user.slice/user-${uid}.slice/session-${1}.scope"
|
|
|
|
}
|
|
|
|
|
2012-06-14 08:50:36 +00:00
|
|
|
# Check session status using systemd
|
2013-11-13 15:34:59 +00:00
|
|
|
session_ids=$(loginctl list-sessions 2>/dev/null | awk '{print $1}')
|
2012-06-14 08:50:36 +00:00
|
|
|
for session in ${session_ids} ; do
|
2017-01-18 15:36:30 +00:00
|
|
|
session_status=$(loginctl show-session ${session})
|
|
|
|
session_processes="$(get_session_processes ${session})"
|
|
|
|
echo "${session_status}" | grep -e 'Active=yes' &> /dev/null &&
|
|
|
|
echo "${session_processes}" | grep -e '\(gnome-settings-daemon\|cinnamon-settings-daemon\|kded[4-5]\|plasmashell\|xfce4-power-manager\|mate-power-manager\)' &> /dev/null && exit 0
|
2012-06-14 08:50:36 +00:00
|
|
|
done
|
|
|
|
|
|
|
|
# Get the ID of the first active X11 session: using ConsoleKit
|
2008-11-11 14:36:30 +00:00
|
|
|
uid_session=$(
|
2012-06-14 13:03:01 +00:00
|
|
|
ck-list-sessions 2>/dev/null | \
|
2008-11-11 14:36:30 +00:00
|
|
|
awk '
|
|
|
|
/^Session[0-9]+:$/ { uid = active = x11 = "" ; next }
|
|
|
|
{ gsub(/'\''/, "", $3) }
|
|
|
|
$1 == "unix-user" { uid = $3 }
|
|
|
|
$1 == "active" { active = $3 }
|
|
|
|
$1 == "x11-display" { x11 = $3 }
|
|
|
|
active == "TRUE" && x11 != "" {
|
|
|
|
print uid
|
|
|
|
exit
|
|
|
|
}')
|
|
|
|
|
|
|
|
# Check that there is a power manager, otherwise shut down.
|
|
|
|
[ "$uid_session" ] &&
|
|
|
|
ps axo uid,cmd | \
|
|
|
|
awk '
|
|
|
|
$1 == '$uid_session' &&
|
2016-07-20 14:58:29 +00:00
|
|
|
($2 ~ /gnome-power-manager/ || $2 ~ /kpowersave/ ||
|
2013-11-13 15:34:59 +00:00
|
|
|
$2 ~ /mate-power-manager/ || $2 ~ /xfce4-power-manager/ ||
|
|
|
|
$2 ~ /\/usr\/libexec\/gnome-settings-daemon/ ||
|
2016-07-20 14:58:29 +00:00
|
|
|
$2 ~ /\/usr\/libexec\/cinnamon-settings-daemon/ ||
|
|
|
|
$2 ~ /kded[4-5]/ || $2 ~ /guidance-power-manager/ ||
|
|
|
|
$2 ~ /plasmashell/) \
|
|
|
|
{ found = 1; exit }
|
2008-11-11 14:36:30 +00:00
|
|
|
END { exit !found }
|
|
|
|
' ||
|
|
|
|
shutdown -h now
|
|
|
|
|