我正在使用Ubuntu和upstart& System-V服务。 我需要收集一个脚本:服务状态(启动/停止)和下次启动状态(将/不会在启动时启动)
chkconfig不适合我的情况:
# chkconfig --list|grep lxc
#
# initctl list|grep lxc
shoc-lxc-net stop/waiting
我编写了脚本,它正在像json一样搜索服务状态和输出。但是我找不到如何检查服务是否会启动:对于upstart + system V。
#!/bin/bash
###############
# gether status of services:
# output json/plain text
# init.d & upstart
# weldpua2008@gmail.com
################
SERVICE=
SERVICEDIR="/etc/init.d"
SUDO=""
JSON_OUTPUT="true"
ONLYONESERVICE="false"
usage(){
cat << EOL
USAGE:
$0 -j yes to enable json output [ by default ]
-s yes enable sudo [ not used by default ]
-n servicename (if you want view status of service)
EOL
exit 1
}
##############################################
# program part
#
unset SELECTIVE_SERVICES_STATUS
declare -A SELECTIVE_SERVICES_STATUS
STATUS="?"
while getopts "n:s:j:" option ;do
case $option in
j) if [[ $OPTARG == "true" || $OPTARG == "yes" ]];then
JSON_OUTPUT="true"
else
JSON_OUTPUT="false"
fi
;;
n) ONLYONESERVICE="true"
ONE_SERVICE=$OPTARG
SELECTIVE_SERVICES_STATUS[$ONE_SERVICE]="?";
;;
# to use sudo
s) SUDO=`which sudo`
;;
\?) usage
exit 1
;;
:) usage
exit 1
;;
esac
done
# output for one service option
function output_one_service() {
local COMMA=$1
case "${STATUS}" in
"?") out_state="unknown";;
"+")out_state="started";;
"-")out_state="stopped";;
esac
if [ ${JSON_OUTPUT} == "true" ];then
echo "${COMMA}{ \"name\":\"${SERVICE}\", "
echo " \"state\": \"${out_state}\""
echo "}"
else
echo "${SERVICE} ${out_state}"
fi
}
#############################################
# Changing global variable STATUS with ?/+/-
#############################################
function upstart_get_status(){
STATUS="?"
${SUDO} initctl status $SERVICE 2> /dev/null|grep start &> /dev/null
if [ $? -eq 0 ];then
STATUS="+"
fi
${SUDO} initctl status $SERVICE 2> /dev/null|grep stop &> /dev/null
if [ $? -eq 0 ];then
STATUS="-"
fi
}
#############################################
# Changing global variable STATUS with ?/+/-
#############################################
function system5_get_status(){
STATUS="?"
# TODO: need implement service status
if [ -x "${SERVICEDIR}/${SERVICE}" ]; then
if ! ${SUDO} grep -qs "\Wstatus)" "${SERVICEDIR}/$SERVICE"; then
#printf " %s %-60s %s\n" "[?]" "$SERVICE:" "unknown" 1>&2
#echo "? $SERVICE"
STATUS="?"
# continue
else
out=$(env -i LANG="$LANG" PATH="$PATH" TERM="$TERM" "${SUDO} $SERVICEDIR/$SERVICE" status 2>&1)
if [ "$?" = "0" -a -n "$out" ]; then
#printf " %s %-60s %s\n" "[+]" "$SERVICE:" "running"
#echo "+ $SERVICE"
STATUS="+"
# continue
else
#printf " %s %-60s %s\n" "[-]" "$SERVICE:" "NOT running"
#echo "- $SERVICE"
STATUS="-"
fi
fi
out_temp=`${SUDO} service ${SERVICE} status 2>/dev/null |grep -v "Usage:" |grep -v "usage:"`
echo "${out_temp}"| grep -w 'is not running\|is NOT running' &> /dev/null
if [ $? -eq 0 ];then
STATUS="-"
fi
echo "${out_temp}"| grep -w 'start/running\|is running\|is up and running\|* running\|* running.' &> /dev/null
if [ $? -eq 0 ];then
STATUS="+"
fi
#echo "service ${SERVICE} status ${STATUS}"
fi
}
# only if -n was provided
if [ ${ONLYONESERVICE} == "true" ];then
if [ ${JSON_OUTPUT} == "true" ];then echo "["; fi
COMMA=""
for SERVICE in "${!SELECTIVE_SERVICES_STATUS[@]}";do
upstart_get_status
if [ "${STATUS}" == "?" ];then
system5_get_status
fi
SELECTIVE_SERVICES_STATUS[$ONE_SERVICE]=${STATUS}
output_one_service ${COMMA}
COMMA=","
done
if [ ${JSON_OUTPUT} == "true" ];then echo "]"; fi
exit 0
fi
SERVICES_LIST=` ${SUDO} find ${SERVICEDIR} -type f -printf "%f\n" |grep -Ev "skeleton|README|*.dpkg-dist|*.dpkg-old|rc|rcS|single|reboot|bootclean.sh|functions|halt|killall|single|linuxconf|kudzu|.legacy-bootordering"`
unset GLOBAL_STATUS
declare -A GLOBAL_STATUS
for SERVICE in ${SERVICES_LIST} ; do
system5_get_status
GLOBAL_STATUS[$SERVICE]=${STATUS}
# echo ">$SERVICE ${GLOBAL_STATUS[$SERVICE]}"
done
for SERVICE in `${SUDO} initctl list|cut -d " " -f1|uniq`;do
upstart_get_status
SERVICE_NAME=`echo $SERVICE|cut -d " " -f1`
# check if key exist
if [ "${STATUS}" != "?" ];then
GLOBAL_STATUS[$SERVICE_NAME]=${STATUS}
###############################
# TODO:
# this logic don't work. Seams we must
# gather status throw service status,
# and not with: /etc/init.d/{service} status
# #######################################
#if [ ${GLOBAL_STATUS[${SERVICE_NAME}]+abc} ];then
# if [ ${GLOBAL_STATUS[${SERVICE_NAME}]} == "?" ];then
# GLOBAL_STATUS[$SERVICE_NAME]=${STATUS}
# fi
#else
# GLOBAL_STATUS[$SERVICE_NAME]=${STATUS}
#fi
fi
done
START=""
STOP=""
UNKNOWN=""
for SERVICE_NAME in "${!GLOBAL_STATUS[@]}";do
if [ ${JSON_OUTPUT} == "true" ];then
if [ ${GLOBAL_STATUS[${SERVICE_NAME}]} == "+" ];then
START="${START}, \"${SERVICE_NAME}\":{\"current_state\":\"started\"} "
elif [ ${GLOBAL_STATUS[${SERVICE_NAME}]} == "-" ];then
STOP="${STOP}, \"${SERVICE_NAME}\":{\"current_state\":\"stopped\"}"
else
UNKNOWN="${UNKNOWN}, \"${SERVICE_NAME}\":{\"current_state\":\"unknown\"}"
fi
else
echo "${SERVICE_NAME} ${GLOBAL_STATUS[${SERVICE_NAME}]}"
fi
done
if [ ${JSON_OUTPUT} == "true" ];then
echo "{ \"services\":"
echo " {"
# started services
echo " \"start\": {"
echo ${START:1}
echo " },"
echo " \"stop\": {"
echo ${STOP:1}
echo " },"
echo " \"unknown\": {"
echo ${UNKNOWN:1}
echo " }"
echo " }"
#
echo "}"
fi
cd ${OLDPWD}
exit 0
PS:
抱歉我的英文