我在/etc/init.d中有一个启动脚本,它调用我的主shell脚本来执行。然后,主脚本将写入日志文件。它应该始终附加到现有的日志文件中。
下面是我的设置所发生的事情:
我遗漏了什么?为什么它在作为服务启动时覆盖日志文件?
在这里,初学者脚本:
#!/bin/sh
SPINDOWNCHECK_BINARY="/home/nzbget/hdd_spindown.sh"
start() {
if [ -e "/tmp/spindowncheck.pid" ]; then
## Program is running, exit with error.
echo "Error! spindowncheck is currently running!" 1>&2
exit 1
else
/home/nzbget/hdd_spindown.sh > /var/log/spindowncheck.log &
echo "spindowncheck started"
touch "/tmp/spindowncheck.pid"
fi
}
stop() {
if [ -e "/tmp/spindowncheck.pid" ]; then
## Program is running, so stop it
killall hdd_spindown.sh
rm "/tmp/spindowncheck.pid"
echo "spindowncheck stopped"
else
## Program is not running, exit with error.
echo "Error! spindowncheck not started!" 1>&2
exit 1
fi
}
case "$1" in
start)
start
exit 0
;;
stop)
stop
exit 0
;;
reload|restart|force-reload)
stop
start
exit 0
;;
**)
exit 1
fi
}
esac下面是主要的剧本:
#!/bin/bash
echo "Reading config...." >&2
. /tmp/spindowncheck.conf
logfile='/var/log/spindowncheck.log'
while [ 1 ]
do
i=0
for DRIVE in $drives
do
DATE=`date +"%Y-%m-%d %H:%M:%S"`
RESULT_OLD=${RESULT[i]}
RESULT[$i]=`hdparm -C $DRIVE | grep state`
if [ "$RESULT_OLD" != "${RESULT[i]}" ]
then echo $DATE $DRIVE ${RESULT[i]} >> $logfile
fi
i=$i+1
done
sleep 10
done发布于 2015-10-25 17:05:27
这一行:
/home/nzbget/hdd_spindown.sh > /var/log/spindowncheck.log &破坏日志文件;将>替换为>>。
此外,作为chepner noted,如果您希望执行shell算法,请使用(POSIX兼容的) $((…))表示法,替换:
i=$i+1通过以下方式:
i=$(($i+1))或者使用Bash扩展,例如:
((i=i+1))
((i+=1))
((i++))在双括号内,您可以在赋值周围使用空格。在Bash中,您不必显式地将$放在变量名的前面(除非取消引用数组元素或…)。在双括号内的赋值中,您必须在LHS变量名之前省略$,就像在shell分配中一样。
https://stackoverflow.com/questions/33330962
复制相似问题