微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决从以 root 身份执行的脚本运行 awesome-client

在 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 代码的路径添加$LUA_PATH 中的 ~/.xsessionrc。鉴于下面描述的错误,我怀疑这是一个问题。

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

的脚本中
#!/bin/bash

if [ "${1}" == "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 [ "${1}" == "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.

鉴于我已经告诉它运行 Awesome 的 $disPLAY(这是一台笔记本电脑),并且我以我的用户而非 root 用户身份运行 awesome-client,还有什么是我错过了阻止它工作的原因?

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

解决方法

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

鉴于我已经告诉它运行 Awesome 的 $DISPLAY(这是一台笔记本电脑),并且我正在以我的用户而不是 root 用户身份运行 awesome-client,我还缺少什么保持这个从工作?

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

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

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

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

Google 建议已经有 PrepareForSleep 信号:

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

基于此,Google 然后给了我以下 AwesomeWM lua 代码,用于监听 logind 的 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,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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。