运行 来自以 root 身份执行的脚本的 awesome-client

Running awesome-client from a script executing as root

运行 Debian (11) 测试很棒

awesome v4.3 (Too long)
 • Compiled against Lua 5.3.3 (running with Lua 5.3)
 • D-Bus support: ✔
 • execinfo support: ✔
 • xcb-randr version: 1.6
 • LGI version: 0.9.2

我正试图在 systemd 触发挂起时向 Awesome 发出信号。在直接摆弄 D-Bus 一段时间却一无所获之后,我写了几个函数,它们在某种程度上复制了信号的功能。

我在 Awesome 会话中通过 运行 以下命令在 shell 中对其进行了测试:

$ awesome-client 'require("lib.syskit").signal("awesome-client", "Hello world!")'

这运行得很好。通知发布到桌面“Hello world!”正如预期的那样。我将 lib.syskit 代码的路径添加到 ~/.xsessionrc 中的 $LUA_PATH。鉴于下面描述的错误,我怀疑这是一个问题。

现在是更困难的部分。我将以下内容放在位于 /lib/systemd/system-sleep/pre-suspend.sh

的脚本中
#!/bin/bash

if [ "" == "pre" ]; then
    ERR=$(export DISPLAY=":0"; sudo -u naddan awesome-client 'require("lib.syskit").signal("awesome-client", "pre-suspend")' 2>&1)
    echo "suspending at `date`, ${ERR}" > /tmp/systemd_suspend_test
elif [ "" == "post" ]; then
    ERR=$(export DISPLAY=":0"; sudo -u naddan awesome-client 'require("lib.syskit").signal("awesome-client", "post-suspend")' 2>&1)
    echo "resuming at `date`, ${ERR}" >> /tmp/systemd_suspend_test
fi

这是写入 /tmp/systemd_suspend_test

的输出
suspending at Thu 22 Jul 2021 10:58:01 PM MDT, Failed to open connection to "session" message bus: /usr/bin/dbus-launch terminated abnormally without any error message                                        
E: dbus-send failed.
resuming at Thu 22 Jul 2021 10:58:05 PM MDT, Failed to open connection to "session" message bus: /usr/bin/dbus-launch terminated abnormally without any error message
E: dbus-send failed.

考虑到我已经告诉它 $DISPLAY Awesome 在 运行 下(这是一台笔记本电脑),而我 运行 awesome-client作为我的用户,而不是 root,我还缺少什么使它无法正常工作?

有没有更好的方法可以在系统挂起时告诉Awesome?

awesome-client 是一个 shell 脚本。它是 dbus-send 的薄包装。因此,既然你写的是“在直接摆弄 D-Bus 一段时间后一无所获”,我想同样的推理也适用。

Given that I'm already telling it the $DISPLAY that Awesome is running under (this is a laptop), and that I'm running awesome-client as my user, not root, what else am I missing that's keeping this from working?

您缺少 dbus 会话总线的地址。对我来说,它是:

$ env | grep DBUS
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus

Is there a better way that I could achieve telling Awesome when the system suspends?

您可以使用现有的机制,而不是通过一些脚本直接向 awesome 发送消息:Dbus 信号。这些是有兴趣的人可以收听的广播。

Google表示已经有PrepareForSleep信号:

https://serverfault.com/questions/573379/system-suspend-dbus-upower-signals-are-not-seen

基于此,Google 然后给了我以下 AwesomeWM lua 代码,用于侦听登录的 PrepareForSleep 信号(由您真实编写 - 感谢 Google 找到那个!):

https://github.com/awesomeWM/awesome/issues/344#issuecomment-328354719

local lgi = require("lgi")
local Gio = lgi.require("Gio")

local function listen_to_signals()
    local bus = lgi.Gio.bus_get_sync(Gio.BusType.SYSTEM)
    local sender = "org.freedesktop.login1"
    local interface = "org.freedesktop.login1.Manager"
    local object = "/org/freedesktop/login1"
    local member = "PrepareForSleep"
    bus:signal_subscribe(sender, interface, member, object, nil, Gio.DBusSignalFlags.NONE,
    function(bus, sender, object, interface, signal, params)
        -- "signals are sent right before (with the argument True) and
        -- after (with the argument False) the system goes down for
        -- reboot/poweroff, resp. suspend/hibernate."
        if not params[1] then
            -- This code is run before suspend. You can replace the following with something else.
            require("gears.timer").start_new(2, function()
                mytextclock:force_update()
            end)
        end
    end)
end

listen_to_signals()