因此,我有一个脚本设置,希望作为服务运行。该脚本将永远连续运行8个curl命令:
#!/bin/bash
threads=8
check_interval=15
batch_url=https://myurl.com
for ((i = 0 ; i < threads ; i++)); do
sleep $i
while true; do
sleep 0.5
has_items=$(curl --silent -m 15.0 --connect-timeout 1.5 $batch_url/$i)
if [ "$has_items" == "false" ]; then
sleep ${check_interval}
continue
fi
done &
done
我在systemd
中设置了一个服务文件,如下所示:
[Unit]
Description=Run the batch forever
After=network.target
[Service]
Type=simple
Restart=always
RestartSec=1
ExecStart=/bin/bash /usr/bin/my_script_location
[Install]
WantedBy=multi-user.target
如果我只是运行脚本,它将运行正常。它将永远循环遍历/0
至/7
的URL。
但是,如果我将其设置为服务,则该服务将在一段时间后重新启动。
我认为该问题与在后台运行while
循环有关,但不确定如何使服务与此配合工作。服务方法可能是错误的方法。我只需要此脚本即可始终保持运行状态。