システムサービスファイルで `xhost`を設定する

システムサービスファイルで `xhost`を設定する

DISPLAY=:0 xhost +システムが起動するたびに実行したいと思います。 (私は安全上の危険を知っていますxhost +。)

このコマンドはローカルで実行され、正常に動作します。

username@hostname:`$ DISPLAY=:0 xhost +
access control disabled, clients can connect from any host

しかし、次のsystemdサービスファイルを介して実行しようとすると、次のように失敗します。

username@hostname:/etc/systemd/system$ cat set-xhost.service 
[Unit]
Description=Set access control for X server

[Service]
Type=oneshot
Environment=DISPLAY=:0
ExecStart=/usr/bin/xhost +

[Install]
WantedBy=multi-user.target
username@hostname:~$ sudo systemctl start set-xhost.service
Job for set-xhost.service failed because the control process exited with error code. See "systemctl status set-xhost.service" and "journalctl -xe" for details.

username@hostname:~$ sudo journalctl -u set-xhost.service
Jun 09 12:09:28 hostname systemd[1]: Starting Set access control for X server...
Jun 09 12:09:28 hostname xhost[7067]: No protocol specified
Jun 09 12:09:28 hostname xhost[7067]: /usr/bin/xhost:  unable to open display ":0"
Jun 09 12:09:28 hostname systemd[1]: set-xhost.service: Main process exited, code=exited, status=1/FAILURE
Jun 09 12:09:28 hostname systemd[1]: Failed to start Set access control for X server.
Jun 09 12:09:28 hostname systemd[1]: set-xhost.service: Unit entered failed state.
Jun 09 12:09:28 hostname systemd[1]: set-xhost.service: Failed with result 'exit-code'.

その理由は何ですか?私が知っている限り、これはうまくいくはずです。うまくいかないということは、私がこの分野に関するいくつかの重要な知識を見逃していることを意味します。

ベストアンサー1

たとえば、依存関係を指定していないため、After=<the service that starts your X11 server>systemd は通常、起動時にできるだけ早くサービスを実行します。

「できるだけ早く」は、実際のX11サーバーが起動する前である可能性が高いです。そして、xhost実行中のX11サーバーに接続できる場合にのみ機能します。

おすすめ記事