我有一个在Red Hat Enterprise Linux 7上使用Type=notify
的服务单元和bash脚本的简单例子,我正在尝试让它工作。
当服务单元配置为以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列表中没有出现
time.sleep(5)
。我将其删除后发现有时通知无法正常工作,因为超时发生了。我很困惑是否是因为删除了sleep
导致的。sleep
真的必要吗? - Mariusz