在系统入睡之前,如何执行脚本?

发布于 2025-02-03 19:58:55 字数 627 浏览 4 评论 0原文

在系统入睡之前,如何在Linux中运行可以访问kwallet的脚本?

在这里,我提供更多详细信息: 我正在寻找一种执行(用户)脚本的机制,该脚本在睡眠/休眠之前运行。

我最初尝试了Systemd(System)服务,Wantyby supsend.target.targethibernate.targethybrid-seep.target < /代码>。

问题在于此服务无法访问kwallet,因为它是系统服务(实际上Kwallet需要用户DBUS上下文)。

下一个想法是运行SystemD用户服务。用户服务可以访问kwallet,但不能与suppend.target.target或之前提到的任何其他中提到的任何一个相关。

后来,建议我使用logind抑制剂钩。从我阅读的内容来看,可以通过程序来抑制或延迟过渡到睡眠模式,直到执行某些代码为止。很像锁。但是,我找不到使用简单的bash脚本进行此类挂钩的教程或示例。如果有人可以向我指出一些文章,或者举一个例子,我将感谢我该怎么做。

How can I run a script in linux that can access kwallet, before the system goes to sleep?

Here I give more details:
I'm looking for a mechanism to execute a (user) script that runs before sleep/hibernate.

I initially tried a systemd (system) service, WantedBy suspend.target, hibernate.target, and hybrid-sleep.target.

The problem is that this service does not have access to kwallet since it is a system service (actually kwallet requires the user dbus context).

The next idea was to run a systemd user service. An user service has access to kwallet, but cannot be tied to suspend.target or any of the others mentioned before.

I've been later advised to use a logind inhibitor hook. From what I read, this mechanism can be used by programs for inhibiting or delaying the transition to sleep mode, until some code is executed. Much like a lock. However, I do not find a tutorial or example that does this kind of hook with a simple bash script. I would appreciate if somebody, could point me to some article, or give an example of how could I do this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫九 2025-02-10 19:58:55

这篇文章

这个想法是创建SystemD - 用户目标,这些目标是通过聆听用于睡眠或锁定的系统事件(通过DBUS消息)来提出的,然后使用此目标创建这些目标想要的另一个用户服务。这已在

最初,我对缺乏系统目标创建系统目标的用户服务的方式感到惊讶。但是后来,我意识到自己的惊喜是由于我对Systemd的工作原理的无知。 SystemD System Services由单个根部进程执行,而SystemD用户服务在单独的用户进程中运行,并且没有直接的方法将它们链接在一起。有了这种理解,我的解决方案像合理的接缝一样。

我感谢阅读这篇文章并想到的人们。

I've found a plausible solution for this problem, as suggested in the answer to this post.

The idea is to create systemd --user targets that are rised by listening to system events for sleep or lock, though dbus messages, and then use this targets to create another user service wanted by these targets. This has been implemented in this repository.

Initially, I was somewhat surprised by the absence of a systemd's native way to create user services wanted by system targets. But then, I realized my surprise was due to my ignorance of how systemd works. Systemd system services are executed by a single root process, while systemd user services run in separate user processes, and there is no straightforward way to link them together. With this understanding, the solution I found seams like a reasonable one.

I thank the people that read this post and gave a thought.

人生百味 2025-02-10 19:58:55

建议阅读此手册页:

https .man7.org/linux/man-pages/man8/systemd-sleep.8.html

特别是本节:

进入系统暂停和/或冬眠之前
systemd-suspend.service(以及其他提到的单元,
分别将运行所有可执行文件
/usr/lib/systemd/system-sleep/,然后向他们传递两个参数。这
第一个论点将是“ pre”,第二个论点是“暂停”,
“ Hibernate”,“杂交睡眠”或“暂停”,然后取决于
选择的动作。一个称为环境变量
将设置“ Systemd_sleep_action”并包含睡眠动作
正在处理。这主要有助于“暂停 - 然后 - 靠水”
变量值将“暂停”,“休眠”或
在冬眠的情况下
失败的。离开系统后立即悬挂和/或休眠
运行相同的可执行文件,但第一个参数现在是“ post”。

该目录中的所有可执行文件均在并行执行,并且
直到所有可执行文件都有
完成。

root可以作为其他用户运行脚本

/tmp/test.sh

#!/bin/bash
  echo "id: $(id)"
  echo "home: $HOME"
  echo "path: $PATH"
  echo "logname: $(logname)"
  echo "date: $(date)"

root Runs /tmp/test.sh作为用户u24

sudo -u u24 /tmp/test.sh

输出:


id: uid=1000(u24) gid=1000(u24) groups=1000(u24),4(adm),190(systemd-journal) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
home: /home/u24
path: /sbin:/bin:/usr/sbin:/usr/bin
logname: root
date: Mon Jun  6 08:28:08 UTC 2022

Suggesting to read this manual page:

https://www.man7.org/linux/man-pages/man8/systemd-sleep.8.html

Especially this section:

Immediately before entering system suspend and/or hibernation
systemd-suspend.service (and the other mentioned units,
respectively) will run all executables in
/usr/lib/systemd/system-sleep/ and pass two arguments to them. The
first argument will be "pre", the second either "suspend",
"hibernate", "hybrid-sleep", or "suspend-then-hibernate" depending on
the chosen action. An environment variable called
"SYSTEMD_SLEEP_ACTION" will be set and contain the sleep action that
is processing. This is primarily helpful for "suspend-then-hibernate"
where the value of the variable will be "suspend", "hibernate", or
"suspend-after-failed-hibernate" in cases where hibernation has
failed. Immediately after leaving system suspend and/or hibernation
the same executables are run, but the first argument is now "post".

All executables in this directory are executed in parallel, and
execution of the action is not continued until all executables have
finished.

root can runs scripts as other users

/tmp/test.sh

#!/bin/bash
  echo "id: $(id)"
  echo "home: $HOME"
  echo "path: $PATH"
  echo "logname: $(logname)"
  echo "date: $(date)"

root runs /tmp/test.sh as user u24

sudo -u u24 /tmp/test.sh

output:


id: uid=1000(u24) gid=1000(u24) groups=1000(u24),4(adm),190(systemd-journal) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
home: /home/u24
path: /sbin:/bin:/usr/sbin:/usr/bin
logname: root
date: Mon Jun  6 08:28:08 UTC 2022

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文