【发布时间】:2017-03-31 02:35:08
【问题描述】:
我在 Debian 上,我有一个调用 bash 脚本的 systemd 服务。
脚本包含一个无限的 while 循环,因为我需要它每 X 秒无限检查一次。
systemd 服务在遇到“while true; do”行时崩溃。
如果我手动执行,脚本运行良好。 为什么 systemd 不喜欢它?我该怎么办?
这里是服务和脚本。正如我所指出的,“while true; do”之前的 echo 语句会打印出来。 "while true; do" 行之后的 echo 语句不打印。
/etc/systemd/system/stream.service:
[Service]
WorkingDirectory=/home/pi/
ExecStart=/home/pi/joi_main.sh
Restart=no
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=stream_service
User=pi
Group=pi
Environment=NODE_ENV=production
[Install]
WantedBy=multi-user.target
/home/pi/joi_main.sh:
#!/bin/bash -e
today=`/bin/date '+%Y_%m_%d__%H_%M_%S'`
exec 2> "/home/pi/stream_logs/$today.$RANDOM.log"
exec 1>&2
#Wait 120s for system to finish booting
sleep 120
#Initial config
export AUDIODEV=mic_mono
export AUDIODRIVER=alsa
sudo sysctl fs.pipe-max-size=1048576
echo "This line prints"
# Check if video buffer is full every minute. if full, the stream needs to restart
while true; do
echo "This line doesn't"
if grep "100% full" /home/pi/video_buffer_usage.txt; then
echo "Buffer is full!"
# Kill existing processes
pkill -f “raspivid|rec|buffer|ffmpeg”
# Wait 10s
sleep 10
./joi_stream.sh &
fi
sleep 60
done
Journalctl 似乎完全没有帮助,但就是这样。没有错误。为什么“会话关闭”?
Mar 31 02:13:41 raspberrypi sudo[1369]: pi : TTY=unknown ; PWD=/home/pi ; USER=root ; COMMAND=/sbin/sysctl fs.pipe-max-size=1048576
Mar 31 02:13:41 raspberrypi sudo[1369]: pam_unix(sudo:session): session opened for user root by (uid=0)
Mar 31 02:13:41 raspberrypi sudo[1369]: pam_unix(sudo:session): session closed for user root
(请不要告诉我为这个while循环启动另一个systemd服务。我希望它成为这个主脚本的一部分,因为它需要在其他所有内容之后运行,如果我关闭主服务我也不希望 while 循环运行,因此维护两个 systemd 服务只会增加麻烦。)
【问题讨论】:
-
请包含您的服务定义文件。
-
我已经完成了,如果我可以添加任何其他内容,请告诉我
-
如果改成
while :; do呢? -
:是一个始终成功的内置命令,因此这避免了必须找到/bin/true才能执行。 -
Barmar:我试过了,仍然没有达到 echo "This line doesn't"。而 /bin/true;做也行不通。