linux启动脚本问题
目前遇到了一个linux启动脚本的问题,有多个服务用到了同一个脚本,但只有2个服务能够正确的返回status的信息,其他服务调用service xxxx status后返回的信息不正确 哪怕该服务已经停掉了 依然返回了它的pid 用./的方式执行一样的情况 但用 bash /etc/init.d/xxx 执行却是OK的 下面是脚本代码
#! /bin/bash
#
# ENMC 服务启动脚本。
#
# Source function library
if [ -f /etc/init.d/functions ] ; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
. /etc/rc.d/init.d/functions
else
exit 1
fi
ENMC_SERVER=enmc_server
$abs_path
get_absolute_path_of_this_script ()
{
ori=$1
ori_dir=`pwd`
while [ -h $ori ]
do
ori=$(readlink $ori)
done
cd $(dirname $ori)
abs_path=`pwd`
cd $ori_dir
}
check_enmc_start ()
{
local prg
prg=${ENMC_SERVER//./\\.}
`ps -o cmd -C $ENMC_SERVER | grep -q "$prg"`
return $?
}
enmc_pid=0
get_enmc_pid ()
{
local prg
prg=${ENMC_SERVER//./\\.}
enmc_pid=$(ps -o pid,cmd -C $ENMC_SERVER | grep "$prg" | gawk '{print $1}')
if [ -z "$enmc_pid" ] ; then
enmc_pid=-1
fi
return 0
}
get_absolute_path_of_this_script $0
cd $abs_path
if [ ! -e "$abs_path/../bin/$ENMC_SERVER" ]; then
echo "$abs_path/../bin/$ENMC_SERVER does not exist."
exit 1
fi
status() {
check_enmc_start
if [ "$?" = "0" ] ; then
get_enmc_pid
echo "$ENMC_SERVER (pid:$enmc_pid) started."
else
echo "$ENMC_SERVER stopped."
fi
return $ret
}
KIND="ENMC"
start() {
echo -n $"Starting $KIND services: "
daemon $abs_path/../bin/$ENMC_SERVER "> /dev/null&"
RETVAL=$?
sleep 5
echo
return $RETVAL
}
stop() {
echo -n $"Shutting down $KIND services: "
killproc $abs_path/../bin/$ENMC_SERVER "> /dev/null"
RETVAL=$?
echo
return $RETVAL
}
restart() {
stop
start
}
# See how we were called.
case "$1" in
start)
start
RETVAL=$?
;;
stop)
stop
RETVAL=$?
;;
restart)
restart
RETVAL=$?
;;
status)
status
RETVAL=$?
;;
save)
save
RETVAL=$?
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
RETVAL=2
esac
exit $RETVAL