#!/bin/bash#
#This shell script takes care of starting and stopping#the Bw system.#
#chkconfig: - 99 01#description: Bw Service.#processname: BwService#pidfile: /var/run/BwService.pid#version: V3.0.0.0
myname=`whoami`
pid_file=bwService.pid#. /lib/lsb/init-functions
python_prog=python3
svc_arg=/home/bw/repo/switch_ssh.py
web_arg="/opt/zabbix3d/manage.py runserver 0.0.0.0:8000"makeAbsolute() {
case $1 in
/*)#already absolute, return it
echo "$1";;*)#relative, prepend $2 made absolute
echo `makeAbsolute "$2" "$PWD"`/"$1" | sed 's,/\.$,,';;
esac
}
getlibdir(){for element in `ls $1`
do
dir_or_file=$1"/"$elementif [ -d $dir_or_file ]
then
libdir="$libdir:$dir_or_file"getlibdir $dir_or_file
fi
done
}
BOOTUP=color
RES_COL=60MOVE_TO_COL="echo -en \\033[${RES_COL}G"SETCOLOR_SUCCESS="echo -en \\033[1;32m"SETCOLOR_FAILURE="echo -en \\033[1;31m"SETCOLOR_NORMAL="echo -en \\033[0;39m"
#define some functions to be used by most or all shell scripts#-------------------------------functions start------------------------------
echo_success() {
txt="OK"
if [ ! "$1" = ""]; then
txt="$1"fi
["$BOOTUP" = "color" ] &&$MOVE_TO_COL
echo-n "["["$BOOTUP" = "color" ] &&$SETCOLOR_SUCCESS
echo-n $"$txt"["$BOOTUP" = "color" ] &&$SETCOLOR_NORMAL
echo-n "]"echo-e "\r"
return0
}
echo_failure() {
txt="FAILED"
if [ ! "$1" = ""]; then
txt="$1"fi
["$BOOTUP" = "color" ] &&$MOVE_TO_COL
echo-n "["["$BOOTUP" = "color" ] &&$SETCOLOR_FAILURE
echo-n $"$txt"["$BOOTUP" = "color" ] &&$SETCOLOR_NORMAL
echo-n "]"echo-e "\r"
return 1}#test -x $PROGRAM || exit 0
#Check if $pid (could be plural) are running
checkpid() {
local ifor i in $*; do
[-d "/proc/$i" ] && return0
donereturn 1}#get the pid
getpid() {
ps-ef|grep -v grep| grep -v '/usr/bin' |grep "$python_prog $1" | awk '{print $2}'
#cmd="ps -ef|grep -v grep |grep \""#for arg in $*; do#cmd=${cmd}" "${arg}#done#cmd="${cmd}\" | awk '{print \$2}'"#echo $cmd#pid=`$cmd`#echo $$#pid=$(ps -ef|grep $1|grep "$2"|awk '{printf $2}')#echo $pid
#if [ -n $2 ]; then
#ps -ef|grep "$1" |grep "$2"| awk '{print $2}'
#else
#ps -ef|grep "$1" | awk '{print $2}'
#fi
}#get the port
getport()
{
grep"\sPORT_NUM" "$path" | grep -Eo "[0-9]+"}#get root user
get_root_user()
{if [ "x" = "x$ners_user" ] || [ "$ners_user" = "root"]; then
ners_user=`ls -ld "$ners_home/." 2>/dev/null | awk 'NR==1{print $3}'`
export ners_user
fi
}#get the pid username
getpiduser()
{
ps-eo user,pid,args | grep -F "$path" | grep "$prog_path/$1" | awk '$3 !~/^[su|-]/{print $1}'}#A function to stop a program.
killproc() {
RC=0#Test syntax.
if [ "$#" -eq 0 ]; then
echo $"Usage: killproc {program} [signal]"
return 1fi
notset=0#check for second arg to be kill level
if [ -n "$2"]; then
killlevel=$2
elsenotset=1killlevel=""fi
pid=`getpid $1`
echo $pid#Kill it.
if [ -n "${pid:-}"] ; then
["$BOOTUP" = "verbose" -a -z "$LSB" ] && echo -n "$2"
if [ "$notset" -eq "1"] ; then#TERM first, then KILL if not dead
kill -TERM $pidwhilecheckpid $pid; do
sleep1done
checkpid $pid
RC=$?
["$RC" -eq 0 ]
RC=`expr 1 -$RC `#use specified level only
else
ifcheckpid $pid; then
kill-9$pidwhilecheckpid $pid; do
sleep1done
RC=$?
["$RC" -eq 0 ]
fi
fielseecho $"$1 shutdown"RC=1fi#Remove pid file if any.
if [ "$notset" = "1"]; then
rm-f /var/run/$pid_file
fireturn$RC
}
start(){
pid=`getpid $2`if [ -n "$pid"]
then
echo"$1 服务已经在运行."
return 1fi
echo-n $"启动$1服务:"dirPath=`dirname $2`
nohup $python_prog $2 $3 $4 >> "$dirPath/output.log" 2>&1 &ret=$?if [ $ret -eq 0 ]; then
sleep5pid=`getpid $2`if [ -n "$pid"]; then
echo_successelseecho_failure
cat"$dirPath/output.log"
return 1fielseecho_failure
cat"$dirPath/output.log"
return 1fiif [ $ret -eq 0 ] && [ -w "/var/run/"]
then
touch/var/run/$pid_file
pid=`getpid $2`
echo $pid> /var/run/$pid_file
fireturn$ret
}
stop(){
pid=`getpid $2`
dirPath=`dirname $2`if [ -n "$pid"]; then
echo-n $"停止$1服务:"killproc $2 > "$dirPath/output.log" 2>&1ret=$?if [ $ret -eq 0 ]; then
echo_successelseecho_failure
cat"$dirPath/output.log"fiif [ $ret -eq 0 ] && [ -w "/var/run/$pid_file"]
then
rm-f /var/run/$pid_file
fielseecho-n "停止$1服务"echo_successreturn0
fireturn$ret
}
startSvc(){
start"交换机"$svc_arg
start"网站"$web_arg
}
stopSvc(){
stop"交换机"$svc_arg
stop"网站"$web_arg
}
restart(){
stopSvc
startSvc
}
restartWeb(){
stopSvc
startSvc
}
list(){
pid=`getpid $2`if [ -n "$pid"]
then
echo-n "$1服务"echo_success"正在运行"
elseecho-n "$1服务"echo_failure"停止运行"fireturn$ret
}
case"$1" instart)
start"交换机"$svc_arg
start"网站"$web_arg
;;
stop)
stop"交换机"$svc_arg
stop"网站"$web_arg
;;
startswitch)
start"交换机"$svc_arg
;;
stopswitch)
stop"交换机"$svc_arg
;;
startweb)
start"网站"$web_arg
;;
stopweb)
stop"网站"$web_arg
;;
restart)
restart
;;
status)
list"交换机"$svc_arg
list"网站"$web_arg
;;*)
exit1esac
exit 0