我写了一个脚本来检查 ntp 服务。它更新它,然后启动服务。
我在下面的脚本中做了三件事:
- 检查服务是否配置正确并且它正在运行。然后,只需退出脚本。即什么也不做。
- 如果服务配置正确,则只需启动服务并退出脚本。
- 如果服务没有配置,则停止服务->配置服务->启动服务。
从上述场景中,场景 1 和场景 3 工作正常。但是在测试场景 2 时,它会引发以下错误:
~ # sh ntpConfigure.sh ntpConfigure.sh: line 3: NTP_CONF.BAK=/etc/ntp.conf.backup: not found ntpd is not running /etc/init.d/ntpd is NOT running server time.myCompany.com / etc/ntp.conf 已配置服务已配置,正在启动 ntp 服务正在启动 ntpd ntpConfigure.sh:第 77 行:echo 服务已成功启动:未找到
知道为什么会抛出此错误吗?我检查了所有内容,但我的代码看起来不错。功能适用于上述错误。有任何想法吗?
代码:
#VERIFY_NTPQ="/bin/ntpq -p"
NTP_CONF="/etc/ntp.conf"
NTP_CONF.BAK="/etc/ntp.conf.backup"
NTPQ_SERVICE="/etc/init.d/ntpd"
TOUCH="/bin/touch"
GREP="/bin/grep"
CP="/bin/cp"
CHKCONFIG="/bin/chkconfig"
$NTPQ_SERVICE status
if [ $? -eq 0 ]
then
isSERVICE_RUNNING=true
echo "$NTPQ_SERVICE is running"
else
isSERVICE_RUNNING=false
echo "$NTPQ_SERVICE is NOT running"
fi
$TOUCH $NTP_CONF
$GREP "server" $NTP_CONF
if [ $? -eq 0 ]
then
isSERVICE__CONFIGURED=true
echo "$NTP_CONF is configured"
else
isSERVICE__CONFIGURED=false
echo "$NTP_CONF is NOT configured"
fi
if $isSERVICE_RUNNING && $isSERVICE__CONFIGURED
then
echo "ntp service is already configured, nothing to do"
exit 0
elif $isSERVICE__CONFIGURED
then
echo "Service is configured already, starting ntp service"
$NTPQ_SERVICE start
if [ $? -eq 0 ]
then
echo" Service is started successfully"
exit 0
else
echo "Failed to start service"
exit 1
fi
else
echo "$NTP_CONF not configured, Configuring $NTP_CONF"
echo "Stopping ntp service "
$NTPQ_SERVICE stop
echo "Taking backup of $NTP_CONF file to $NTP_CONF.BAK"
$CP $NTP_CONF $NTP_CONF.BAK
echo "Updating $NTP_CONF file with the required configurations"
echo "restrict 127.0.0.1" > $NTP_CONF
echo "restrict default kod nomodify notrap" >> $NTP_CONF
echo "driftfile /etc/ntp.drift" >> $NTP_CONF
echo "server time.myCompany.com" >> $NTP_CONF
if [ $? -eq 0 ]
then
echo "$NTP_CONF is configued properly"
echo "Starting ntp service"
$NTPQ_SERVICE start
if [ $? -eq 0 ]
then
echo "ntp service configured and started successfully"
echo "Configuring ntp service to start at bootup"
$CHKCONFIG ntpd on
exit 0
else
echo "Failed to start ntp service"
exit 1
fi
else
echo "Failed to configure ntp service"
exit 1
fi
fi