首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从systemd用户服务访问会话D总线?

如何从systemd用户服务访问会话D总线?
EN

Stack Overflow用户
提问于 2016-07-27 21:51:40
回答 1查看 1.4K关注 0票数 0

我有运行shell脚本的用户服务,它试图访问会话的锁定屏幕状态,如下所示:

代码语言:javascript
复制
# Test Unity screen-lock:
isLocked() {
    isLocked=$(gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked)
}

lock() {
  if [[ $isLocked == "(false,)" ]]; then                                                                                                     
        gnome-screensaver-command -l
  elif [[ $isLocked == "(true,)" ]]; then                                                                                                
        exit 1
  fi
exit 0
}

问题是服务“是每个用户的进程,而不是每个会话”,我不知道如何访问会话dbus:

代码语言:javascript
复制
GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name com.canonical.Unity was not provided by any .service files
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-20 03:43:25

我也面临着同样的问题,而我的研究却一无所获。如果有人能找到更好的方式来查询联合的锁状态,我很想听听它。

我最近了解了命名管道,当时我想知道我可能会用这样的东西做什么。事实证明,这个问题恰好是一个完美的应用程序。

创建以下脚本..。

代码语言:javascript
复制
#!/bin/bash
# this script is meant to be called in two different ways.
# 1.    It can be called as a service by passing "service" as the first argument. In this 
#   mode, the script listens for requests for lock screen status. The script should be
#   run in this mode with startup applications on user logon.
# 2.    If this script is called without any arguments, it will call the service to request
#   the current lock screen status. Call this script in this manner from your user 
#   service.

# this will be the named pipe we'll use for requesting and receiving screen lock status
pipe="/tmp/lockscreen-status"

if [ "$1" == "service" ]; then
    # setup the named pipe
    rm "$pipe"
    mkfifo "$pipe"

    # start watching for requests
    while true
    do
        # watch the pipe for trigger events requesting lock screen status
        read request < "$pipe"

        # respond across the same pipe wit the current lock screen status
        gdbus call -e -d com.canonical.Unity -o /com/canonical/Unity/Session -m com.canonical.Unity.Session.IsLocked | grep -oP "(true)|(false)" > "$pipe"
    done
else
    # make sure the pipe exists
    if [ ! -e "$pipe" ]; then
        echo "This script must started in service mode before lock status can be queried."
        exit
    fi

    # send a request for screen lock status and read the response
    touch "$pipe"
    read status < "$pipe"

    [ "$status" == "" ] && status="This script must started in service mode before lock status can be queried."

    # print reponse to screen for use by calling application
    echo $status
fi

正如注释中所指出的,您需要以两种方式调用此脚本。当用户登录时,以服务模式启动此脚本。我使用“启动应用程序”程序来调用脚本,但是它的调用方式并不重要,只要它在登录时被用户帐户调用就行了。然后,在您的用户服务中,只需调用此脚本,如果屏幕被锁定,它将返回"true“,如果屏幕被解锁,则返回"false”。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38623791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档