運行服務 spacenavd start 與 /etc/init.d/spacenavd start 不同

運行服務 spacenavd start 與 /etc/init.d/spacenavd start 不同

我有一個使用 3dconnexion 太空滑鼠的應用程式。使用守護程序時它工作正常,spacenavd但是我在讓守護程序在啟動時運行或作為服務運行時遇到問題。

$ sudo 服務 spacenavd 啟動

在 /var/log/spnavd.log 中

Spacenav daemon 0.5
failed to open config file /etc/spnavrc: No such file or directory. using defaults.
Device detection, parsing /proc/bus/input/devices
using device: /dev/input/event5
device name: 3Dconnexion SpaceNavigator
trying to open X11 display ":0.0"
XAUTHORITY=/root/.Xauthority
No protocol specified
No protocol specified
failed to open X11 display ":0.0"
waiting for X socket file to appear

但是,當我運行 system V init 腳本時,沒有任何問題:

$ sudo /etc/init.d/spacenavd 啟動

在 /var/log/spnavd.log 中

Spacenav daemon 0.5
failed to open config file /etc/spnavrc: No such file or directory. using defaults.
Device detection, parsing /proc/bus/input/devices
using device: /dev/input/event5
device name: 3Dconnexion SpaceNavigator
trying to open X11 display ":0"
XAUTHORITY=/home/sam/.Xauthority

問題是我需要守護程序從引導啟動,而不是手動啟動它,這需要 sudo 存取權限。

編輯(muru的建議)

我製作了以下暴發戶腳本

description "Spacenavd upstart script. Starts after lightdm to allow for binding with the Xorg server"
author "Samuel Charreyron"

start on started lightm
stop on shutdown

pre-start script
    echo "Starting spacenavd daemon"
end script

script 
    env DISPLAY=:0.0
    exec /usr/sbin/spacenavd -v -d 2> /var/log/spnavd.log
end script

pre-stop script
    echo "Stopping spacenavd daemon"
    # detect daemon's process id
    pid=`cat /var/run/spnavd.pid 2>/dev/null`
    if [ $? != 0 ]; then
        pid=`ps -e | grep spacenavd | awk '{ print $1 }'`
        if [ -z "$pid" ]; then
             echo 'spacenavd daemon is not running, nothing to do.'
             exit 1
        fi
    fi
    kill $pid
end script

我看到 spacenavd 無法連接到 Xorg 伺服器。事實上,任何由 root 執行的 upstart 腳本似乎都無法使用 Xorg。由於 spacenavd 需要 sudo 存取 USB 設備,我該怎麼做?

答案1

預設情況下,root 無權存取 Xserver。為了啟用它,您需要將 /home/sam/.Xauthority 檔案複製到 /root/.Xauthority

sudo cp /home/sam/.Xauthority /root/

重新啟動後,只需在啟動時啟用 spacenavd 守護程式:

sudo service spacenavd enable

並啟動 spacenavd 服務:

sudo service spacenavd start

相關內容