簡體   English   中英

systemctl 服務 systemd-notify 不適用於非 root 用戶

[英]systemctl service systemd-notify not working with non-root user

我在 Red Hat Enterprise Linux 7 上有一個簡單的服務單元和 bash 腳本示例,使用Type=notify我正在嘗試開始工作。

當服務單元配置為以 root 身份啟動腳本時,事情會按預期工作。 添加User=testuser時失敗。 當腳本最初啟動時(如進程列表所示), systemctl服務永遠不會收到指示就緒的通知消息,因此它掛起並最終超時。

[Unit]
Description=My Test
[Service]
Type=notify
User=testuser
ExecStart=/home/iatf/test.sh
[Install]
WantedBy=multi-user.target

Test.sh(由具有執行權限的 testuser 擁有)

#!/bin/bash

systemd-notify --status="Starting..."
sleep 5
systemd-notify --ready --status="Started"

while [ 1 ] ; do
  systemd-notify --status="Processing..."
  sleep 3
  systemd-notify --status="Waiting..."
  sleep 3
done

當以 root 身份運行時,systemctl status test 顯示從我的 test.sh bash 腳本發送的正確狀態和狀態消息。 User=testuser服務掛起然后超時和journalctl -xe報告:

Jul 15 13:37:25 tstcs03.ingdev systemd[1]: Cannot find unit for notify message of PID 7193.
Jul 15 13:37:28 tstcs03.ingdev systemd[1]: Cannot find unit for notify message of PID 7290.
Jul 15 13:37:31 tstcs03.ingdev systemd[1]: Cannot find unit for notify message of PID 7388.
Jul 15 13:37:34 tstcs03.ingdev systemd[1]: Cannot find unit for notify message of PID 7480.

我不確定這些 PID 是什么,因為它們沒有出現在 ps -ef 列表中

這似乎是notify服務類型中的已知限制

拉取請求systemd手冊頁

    Due to current limitations of the Linux kernel and the systemd, this
    command requires CAP_SYS_ADMIN privileges to work
    reliably. I.e. it's useful only in shell scripts running as a root
    user.

我已經與sudo和朋友一起嘗試了一些 hacky 解決方法,但它們不會像systemd工作 - 通常會失敗

No status data could be sent: $NOTIFY_SOCKET was not set

這是指systemd-notify試圖將數據發送到的套接字 - 它在服務環境中定義,但我無法將其可靠地暴露給 sudo 環境

您也可以嘗試使用此處描述的 Python 解決方法

python -c "import systemd.daemon, time; systemd.daemon.notify('READY=1'); time.sleep(5)"

它基本上只是一個不可靠的睡眠,使用notify是可靠的服務。

在我的情況下 - 我只是重構為使用root作為用戶 - 將實際服務作為主服務下的子項與所需用戶

sudo -u USERACCOUNT_LOGGED 通知發送“你好”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM