我是脚本/Linux 新手。我一直在为此做一些研究,但现在我陷入困境了。
我运行了一个 Python 脚本,一段时间后出现错误(显示在终端窗口中)。
我需要:
由于我的无知,我这样做了:
#!/bin/sh
#execute the python script as a normal user and make a output.txt file so the grep command can find the "331" word
echo "Starting Script"
python main.py | tee output.txt
#using tail and grep to look for the "331" word:
if [ tail -f /path/to/script/output.txt | grep "331" ]; then
echo "Error found. Killing Process"
killall main.py
echo "Restarting script..."
./startcap2.sh
fi
done
它会启动脚本,但如果出现错误则无法终止/重新启动。
我错过了什么?
感谢您的帮助!
如果你只是 grep 进程本身,并使用
head -n 1
它应该会因为管道损坏而被自动杀死:
#!/bin/sh
echo "Starting Script"
while true
do
python main.py | tee -a output.txt | grep "331" | head -n 1 # run until first line with 331 occurs
echo "Restarting script..."
done