在系统入睡之前,如何执行脚本?
在系统入睡之前,如何在Linux中运行可以访问kwallet的脚本?
在这里,我提供更多详细信息: 我正在寻找一种执行(用户)脚本的机制,该脚本在睡眠/休眠之前运行。
我最初尝试了Systemd
(System)服务,Wantyby supsend.target.target
,hibernate.target
和hybrid-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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如这篇文章。
这个想法是创建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.
建议阅读此手册页:
https .man7.org/linux/man-pages/man8/systemd-sleep.8.html
特别是本节:
root可以作为其他用户运行脚本
/tmp/test.sh
root Runs
/tmp/test.sh
作为用户u24
输出:
Suggesting to read this manual page:
https://www.man7.org/linux/man-pages/man8/systemd-sleep.8.html
Especially this section:
root can runs scripts as other users
/tmp/test.sh
root runs
/tmp/test.sh
as useru24
output: