Bash文档说echo
写入stdout
。我有一个脚本,将stderr
重定向到带有2>/home/pi/mydir/stderr-log.txt
的文件。它不会重定向stdout
。 echo
输出的文本仍将进入日志文件。
通过在命令末尾添加&
可以在后台运行脚本有什么不同吗?
完整的脚本:
#! /bin/sh
case "$1" in
start)
echo "Starting mjpg_streamer!"
#First init
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/mjpg-streamer/input_uvc.so -n -f 30 -r 640x480 -d /dev/video0" -o "/usr/local/lib/mjpg-streamer/output_http.so -p 8085 -w /usr/local/share/mjpg-streamer/www" 2>/home/pi/mydir/stderr-log.txt &
#Second init
/usr/local/bin/mjpg_streamer -i "/usr/local/lib/mjpg-streamer/input_uvc.so -n -f 30 -r 640x480 -d /dev/video1" -o "/usr/local/lib/mjpg-streamer/output_http.so -p 8086 -w /usr/local/share/mjpg-streamer/www" 2>/home/pi/mydir/stderr-log.txt &
;;
stop)
echo "Stopping mjpg_streamer!"
killall mjpg_streamer
;;
*)
echo "usage: videoinit {start|stop}"
exit 1
;;
esac
exit 0
调用该脚本的脚本(当它在日志文件中找到新的错误消息时):
#!/bin/bash
ERRORLOG="/home/pi/mydir/stderr-log.txt"
LINECOUNT=0
NEWLINECOUNT=0
rm ${ERRORLOG}
touch ${ERRORLOG}
while true; do
inotifywait -q -e modify ${ERRORLOG} > /dev/null
NEWLINECOUNT=$(grep --count "Error grabbing frames" ${ERRORLOG} )
if [[ NEWLINECOUNT > LINECOUNT ]]; then
LINECOUNT=NEWLINECOUNT
/etc/init.d/videoinit stop
echo "Attempting mjpg-streamer reboot."
/etc/init.d/videoinit start
fi
done