![](/img/trans.png)
[英]How to output the start and stop datetime of shell script (but no other log)?
[英]Shell script overwriting log at start
我在/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
这行:
/home/nzbget/hdd_spindown.sh > /var/log/spindowncheck.log &
破坏日志文件; 用>>
替换>
。
而且,如chepner 所述 ,如果您希望执行Shell算术运算,请使用(POSIX- compatible ) $((…))
表示法,替换为:
i=$i+1
有:
i=$(($i+1))
或带有Bash扩展名,例如:
((i=i+1))
((i+=1))
((i++))
在双括号内,您可以在作业周围使用空格。 在Bash中,您不必显式地将$
放在变量名的前面(除非您要取消引用数组元素或…)。 在双括号内的赋值中,必须像在shell赋值中一样,在LHS变量名之前省略$
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.