启停脚本

我一直忍受不了没有效率的事情,公司同时负责服务器部署和发布的同事老久都没整一套好的脚本。
所以自己负责新项目的时候整了一套服务启停的脚本,经实践,个人觉得非常好用。所以在这里记录下。

需要依赖daemonize的一个程序
sudo yum install -y daemonize

/etc/init.d/functions 这里面还是有好多好东西的。

start-stop.sh

用来选择是启动还是停止某个进程还是所有的进程

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
# 启停脚本
#
COLOR_RED='\033[0;31m'
COLOR_GREEN='\033[0;32m'
COLOR_RESET='\033[0m'
usage() {
echo "Usage: `basename $0` {start|stop|restart|force-stop|force-restart|status}" >&2
}
# At least three arguments are required.
if [ $# -lt 1 ]; then
usage
exit 5
fi
# 引入参数
. ./parameters.sh
server_len=$((${#SERVERS[@]}))
function choose_server() {
printf "\n========================================\n"
printf " Server Name List\n"
for ((i=0; i<server_len; i=i+1)); do
printf "%-10d %-20s\n" $i ${SERVERS[$(($i))]}
done
printf "========================================\n"
printf " Server Name List\n"
read -p "Please Enter server index[0-$(($server_len -1))](greater than $server_len instend of all): " server_index
if [[ "$server_index" =~ ^[0-9]?$ && $server_index -ge 0 ]]; then
return $server_index
else
echo -e "${COLOR_RED}invalidate input ${COLOR_RESET} : $COLOR_GREEN $server_index $COLOR_RESET";
exit 4;
fi
}
# 单个服还是所有,大于长度代表所有
choose_server
server_id=$?
PROG_NAME=''
PROG_ARGS=''
ACTION=$1
echo "will $ACTION server follow.."
if [ $server_id -lt $server_len ]; then
PROG_NAME=${SERVERS[$server_id]}
PROG_ARGS=${SERVER_PARAM[$PROG_NAME]}
echo "${PROG_NAME} -----> ${PROG_ARGS}"
else
for key in ${SERVERS[@]};do
echo "${key} -----> ${SERVER_PARAM[$key]}"
done
fi
read -n1 -p "Press insure. [y/n]" sure
echo -e "\nYour inputs: $sure"
if [ "$sure" == "y" ]; then
echo "$server_id $server_len"
if [ $server_id -lt $server_len ]; then
echo "$PROG_NAME $ACTION $PROG_ARGS"
#bash single.sh $PROG_NAME $ACTION $PROG_ARGS
else
for key in ${SERVERS[@]};do
PROG_NAME=${key}
PROG_ARGS=${SERVER_PARAM[$key]}
echo "$PROG_NAME $ACTION $PROG_ARGS"
#bash single.sh $PROG_NAME $ACTION $PROG_ARGS
done
fi
fi

single.sh

用来操作单个进程的脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/bash
# 启停脚本
#
usage() {
echo "Usage: `basename $0` PROGRAM_NAME {start|stop|restart|status} PROGRAM_NAME" >&2
echo "Where: PROGRAM_NAME is alias for this process." >&2
echo " PROGRAM_ARG contain the executable file and command lind params." >&2
}
array=( $@ )
len=${#array[@]}
# At least three arguments are required.
if [ $len -lt 3 ]; then
usage
exit 5
fi
source /etc/init.d/functions
CUR_DIR=$(dirname $(realpath $0))
PROGRAM_NAME=${array[0]}
ACTION=${array[1]}
PROGRAM_ARGS=${array[@]:2:$len-1}
RETVAL=0
PID_FILE=".$PROGRAM_NAME.pid"
LOCK_FILE=".$PROGRAM_NAME.pid.lock"
LOG_FILE="$PROGRAM_NAME.log" #标准输出和错误输出
function start() {
daemonize -c $CUR_DIR -p $PID_FILE -l $LOCK_FILE -o $LOG_FILE -e $LOG_FILE -v $PROGRAM_ARGS
RETVAL=$?
[ $RETVAL = 0 ] && success || failure
echo
}
function stop(){
killproc -p $PID_FILE
RETVAL=$?
echo
rm -f $LOCK_FILE
}
function status_(){
status -p $PID_FILE
RETVAL=$?
}
case "$ACTION" in
start)
start;
;;
restart)
stop; sleep 1; start;
;;
stop)
stop
;;
status)
status_
;;
*)
usage
exit 4
;;
esac
exit $RETVAL

parameters.sh

用来配置哪些进程需要启动,以及进程启动的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#!/bin/bash
# 需要启动的服对应的名称和参数
#
CUR_DIR=$(dirname $(realpath $0))
LOGIN="login"
HALL="hall"
COUNT="count"
GAME10206="game10206"
# 定义哪些服需要启动
SERVERS=( \
${LOGIN} \
${HALL} \
${COUNT} \
${GAME10206})
declare -A SERVER_PARAM
SERVER_PARAM=([${LOGIN}]="$CUR_DIR/login --log_dir=log/login" \
[${HALL}]="$CUR_DIR/hall --log_dir=log/hall" \
[${COUNT}]="$CUR_DIR/count --log_dir=log/count" \
[${GAME10206}]="$CUR_DIR/game --log_dir=log/game10206")
for key in ${SERVERS[@]};do
echo "${key} -----> ${SERVER_PARAM[$key]}"
done