如何循环执行output_a=$(/usr/bin/redis-cli -a 123456 ping 2>&1 | awk '{print $(NF-1), $NF}') >>$LOGFILE 2>&1 echo $output_a if [ "$output_a" = "pone" ]; then $REDISCLI SLAVEOF NO ONE >>$LOGFILE 2>&1 echo "pone" else pkill keepalived >>$LOGFILE echo "kill keep" /data/keepaliveds/keepalived -f /data/keepaliveds/keepalived.conf echo "sueesss" fi
时间: 2024-02-22 22:54:47 浏览: 95
通过nohup循环执行该脚本即可不间断执行(单纯的nohup不起作用时)
可以使用while循环来实现对该脚本的循环执行。具体代码如下:
```bash
#!/bin/bash
LOGFILE=/var/log/redis-monitor.log
REDISCLI=/usr/bin/redis-cli
PASSWORD=123456
while true
do
output_a=$($REDISCLI -a $PASSWORD ping 2>&1 | awk '{print $(NF-1), $NF}')
echo $output_a >> $LOGFILE 2>&1
if [ "$output_a" = "pone" ]; then
$REDISCLI -a $PASSWORD SLAVEOF NO ONE >> $LOGFILE 2>&1
echo "pone" >> $LOGFILE
else
pkill keepalived >> $LOGFILE
echo "kill keep" >> $LOGFILE
/data/keepaliveds/keepalived -f /data/keepaliveds/keepalived.conf >> $LOGFILE
echo "sueesss" >> $LOGFILE
fi
sleep 10
done
```
上面的代码将脚本放在了一个无限循环的while循环中,每次执行完毕后,会sleep 10秒钟,然后再次执行该脚本。这样就可以实现对该脚本的循环执行了。当然,你可以根据自己的需求,设置不同的循环时间。
阅读全文