You are not logged in.

#1 2013-09-23 18:51:31

j0lly
Member
From: ~Milan
Registered: 2011-08-16
Posts: 49

suspend with dbus non interctively (cron and xautolock) [SOLVED]

Hello forum,

I'm getting some troubles trying to suspend on low battery within both a crontab script and a xautolock command:

19:52:41 $ cat /usr/local/bin/battery-check.sh

#!/usr/bin/env bash

## check battery and suspend if crytical ##

export DISPLAY=:0

BAT_LVL=$(acpi | cut -d',' -f2| grep -Eo "[0-9]+")
BAT_STATE='acpi -a | grep "off-line"'
LVL=$1


if (( $BAT_LVL < $LVL )) && [[ $BAT_STATE ]]; then
    notify-send -u critical -t 3000 'La batteria é scarica, sto sospendendo..'
    dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend
fi
*/3 * * * * /usr/local/bin/battery-check.sh > /dev/null 2>&1

and within my .i3/config:

exec_always --no-startup-id xautolock -time 5 -locker 'i3lock -d -c 000000' -killtime 10 -killer 'dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend' -detectsleep

is surely something stupid but i can't find any article describing how to work it out.
.xinitrc

exec dbus-launch --sh-syntax --exit-with-session i3 -V -d all > ~/.i3/i3log-$(date +'%F-%k-%M-%S') 2>&1

journalctl

set 23 20:18:01 0x0000 CROND[3074]: (j0lly) CMD (/usr/local/bin/battery-check.sh 30 > /dev/n...>&1)
set 23 20:18:01 0x0000 dbus-daemon[343]: dbus[343]: [system] Activating via systemd: service ...ce'
set 23 20:18:01 0x0000 dbus[343]: [system] Activating via systemd: service name='org.freedes...ice'
set 23 20:18:01 0x0000 systemd[1]: Starting Daemon for power management...
-- Subject: Unit upower.service has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit upower.service has begun starting up.
set 23 20:18:01 0x0000 dbus-daemon[343]: dbus[343]: [system] Successfully activated service '...er'
set 23 20:18:01 0x0000 dbus[343]: [system] Successfully activated service 'org.freedesktop.UPower'
set 23 20:18:01 0x0000 systemd[1]: Started Daemon for power management.
-- Subject: Unit upower.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit upower.service has finished starting up.
--
-- The start-up result is done.
set 23 20:18:01 0x0000 dbus-daemon[343]: dbus[343]: [system] Activating via systemd: service ...ce'
set 23 20:18:01 0x0000 dbus[343]: [system] Activating via systemd: service name='org.freedes...ice'
set 23 20:18:01 0x0000 systemd[1]: Starting Authorization Manager...
-- Subject: Unit polkit.service has begun with start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit polkit.service has begun starting up.
set 23 20:18:01 0x0000 polkitd[3096]: Started polkitd version 0.112
set 23 20:18:01 0x0000 polkitd[3096]: Loading rules from directory /etc/polkit-1/rules.d
set 23 20:18:01 0x0000 polkitd[3096]: Loading rules from directory /usr/share/polkit-1/rules.d
set 23 20:18:01 0x0000 polkitd[3096]: Finished loading, compiling and executing 2 rules
set 23 20:18:01 0x0000 dbus-daemon[343]: dbus[343]: [system] Successfully activated service '...t1'
set 23 20:18:01 0x0000 dbus[343]: [system] Successfully activated service 'org.freedesktop.P...it1'
set 23 20:18:01 0x0000 systemd[1]: Started Authorization Manager.
-- Subject: Unit polkit.service has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit polkit.service has finished starting up.
--
-- The start-up result is done.
set 23 20:18:01 0x0000 polkitd[3096]: Acquired the name org.freedesktop.PolicyKit1 on the sy... bus
set 23 20:18:01 0x0000 CROND[3073]: pam_unix(crond:session): session closed for user j0lly
set 23 20:18:02 0x0000 upowerd[3093]: [powerdown] cannot write powersave to /sys/module/pcie...licy
set 23 20:18:02 0x0000 upowerd[3093]: [powerdown] cannot write 1 to /sys/module/i915/paramet..._rc6

thanks for the hints

j0lly

Last edited by j0lly (2013-09-25 17:50:33)


...why not

Offline

#2 2013-09-23 18:59:01

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: suspend with dbus non interctively (cron and xautolock) [SOLVED]

you really should tell us what exactly isn't working and what you already tried to fix it... either way:

dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend

This is deprecated and will stop working as soon as the new upower version hits [extra], use "systemctl suspend" instead.

exec dbus-launch --sh-syntax --exit-with-session [...]

this may cause problems. use the .xinitrc from /etc/skel which contains a loop to start dbus and other stuff. Then only use "exec i3".

Offline

#3 2013-09-23 19:53:57

j0lly
Member
From: ~Milan
Registered: 2011-08-16
Posts: 49

Re: suspend with dbus non interctively (cron and xautolock) [SOLVED]

Thanks 65kid for your suggestions;
I'll use the skel to start i3 and dbus on the session.

For the suspend command, i tried

systemctl suspend

instead

    dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend

and the error is now:

set 23 21:51:01 0x0000 CROND[22859]: (j0lly) CMD (/usr/local/bin/battery-check.sh 55 > /dev/null 2>&1)
set 23 21:51:01 0x0000 dbus-daemon[343]: dbus[343]: [system] Rejected send message, 2 matched rules; type="method_call", sender=":1.40" (uid=1000 pid=22870 comm="systemctl suspend ") interface="org.freedesktop.systemd1.Manager" member="StartUnit" error name="(unset)" requested_reply="0" destination="org.freedesktop.systemd1" (uid=0 pid=1 comm="/sbin/init ")
set 23 21:51:01 0x0000 dbus[343]: [system] Rejected send message, 2 matched rules; type="method_call", sender=":1.40" (uid=1000 pid=22870 comm="systemctl suspend ") interface="org.freedesktop.systemd1.Manager" member="StartUnit" error name="(unset)" requested_reply="0" destination="org.freedesktop.systemd1" (uid=0 pid=1 comm="/sbin/init ")

what doesn't  work is simply the suspention, it just doesn't work..


...why not

Offline

#4 2013-09-23 20:26:22

firecat53
Member
From: Lake Stevens, WA, USA
Registered: 2007-05-14
Posts: 1,542
Website

Re: suspend with dbus non interctively (cron and xautolock) [SOLVED]

/etc/sudoers

...
%users  ALL = NOPASSWD: /usr/bin/systemctl suspend                                                                                  

your script:

.....
    sudo systemctl suspend

OR
laptop-mode-tools has this feature built in (not the xautolock part, but hibernating/suspending on low battery).

Scott

Offline

#5 2013-09-24 19:01:28

j0lly
Member
From: ~Milan
Registered: 2011-08-16
Posts: 49

Re: suspend with dbus non interctively (cron and xautolock) [SOLVED]

Hello firecat65,
I rather don't like to achieve non interactive shutdown/suspend with sudo, i know it can be done in this way, and indeed it going to work, but what i would like to achive is a proper way to use privleges as per user session.
I can't rely on this...
There's no way to export the dbus session variables?
I couldn't find any solution right away


...why not

Offline

#6 2013-09-24 20:09:03

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: suspend with dbus non interctively (cron and xautolock) [SOLVED]

doesn't cron run the script as root? My knowledge of cron is limited. Assuming that the script is run as your user by cron, this can't work because the cron service is not part of an active session and therefore has no privileges to suspend. You may write a polkit rule to always allow suspend for your user (this is one of the rare cases where writing a polkit rule actually makes sense and isn't used as a crappy workaround for a broken session).

For example, I use the following rule on my server to allow suspend for my user always, so I can suspend via SSH as well:

# /etc/polkit-1/rules.d/00-allow-suspend.rules
polkit.addRule(function(action, subject) {
	if(action.id == "org.freedesktop.login1.suspend" && subject.user == "<your-user-here>") {
		return polkit.Result.YES;
	}
});

Although I have to admit that this isn't much different than using sudo NOPASSWD. I still think it's cleaner since I actually use the authentication mechanism that the systemd interface itself uses (and I don't have to type sudo wink ). You also have a more fine grained control with polkit if you need it (check man polkit).

Offline

#7 2013-09-25 17:49:32

j0lly
Member
From: ~Milan
Registered: 2011-08-16
Posts: 49

Re: suspend with dbus non interctively (cron and xautolock) [SOLVED]

That's brilliant big_smile!

It is persistent and as you said, uses the same framework systemd actulally uses.
Unless countrary, this is the only solution I have found without use of sudo.
I think it can be mark as [SOLVED].

Thanks 65kid


...why not

Offline

Board footer

Powered by FluxBB