-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.sh.sample
executable file
·182 lines (163 loc) · 3.29 KB
/
run.sh.sample
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/bash
PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
# 定时命令
# * * * * * www /bin/sh /web/currentPrice/bin/run.sh care config.ini >> /web/currentPrice/bin/log/care.log 2>&1
# 工作目录
WORKDIR=$(cd $(dirname $0); pwd)
if [ "$(uname)" == "Darwin" ];then
SP="/"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ];then
SP="/"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ];then
SP="\\"
fi
cd $WORKDIR
#日志记录
log()
{
echo `date +%Y-%m-%d`" $1"
}
# Server酱通知(http://sc.ftqq.com/3.version) token
TOKEN=""
BIN="currentPrice.bin"
CONFIG="config.ini"
#read -p "configFile:" CONFIG
if [ -n "$2" ];then
if [ ! -f "$2" ];then
log "configFile: $2 not exist"
exit
fi
CONFIG=$2
fi
# 操作对象
op='none'
if [ -n "$1" ];then
op=$1
fi
# 消息通知函数
notify()
{
curl -o /dev/null -s "https://sc.ftqq.com/${TOKEN}.send?text=${1// /%20}&desp=${2// /%20}";
}
# pika
pikaCount()
{
echo `ps aux | grep -F 'pika' | grep -v -F 'grep' | wc -l`;
}
pikaStart()
{
cd $WORKDIR && pika -c ./pika.conf;
}
# redis
redisCount()
{
echo `ps aux | grep -F 'redis-server' | grep -v -F 'grep' | wc -l`;
}
redisStart()
{
cd $WORKDIR && redis-server ./redis.conf;
}
# currentPrice
count()
{
echo `ps aux | grep -F $BIN | grep -F $CONFIG | grep -v -F 'grep' | wc -l`;
}
# 运行程序
run()
{
#切换到运行目录
cd $WORKDIR
#设置日志存储
if [ ! -d "${WORKDIR}${SP}log" ]; then
mkdir "${WORKDIR}${SP}log"
fi
local DATA_DAY=`date +%Y-%m-%d`
local DATA_SECOND=`date +%Y-%m-%d-%H-%M-%S`
local LOG_NAME="${WORKDIR}${SP}log${SP}${BIN}-$(basename $CONFIG)-${DATA_DAY}.log"
local BACKUP_LOG_NAME="${WORKDIR}${SP}log${SP}${BIN}-$(basename $CONFIG)-${DATA_SECOND}_bak.log"
#备份日志
if [ -a "${LOG_NAME}" ]; then
mv ${LOG_NAME} ${BACKUP_LOG_NAME}
fi
nohup ${WORKDIR}${SP}${BIN} -f ${CONFIG} >> ${LOG_NAME} 2>&1 &
log "启动程序: $BIN -f ${CONFIG} 进程ID: $!"
log "日志存储: ${LOG_NAME} "
log "当前进程数:`count`"
return $!
}
# 开始函数
start()
{
run
return $!
}
# 结束函数
stop()
{
local num=`count`
while [ $num -gt 0 ]; do
log "等待结束进程数:"$num
ps aux | grep -F $BIN | grep -F $CONFIG | grep -v -F 'grep' | awk '{print $2}' | xargs -I {} kill -9 {}
num=`count`
done
log "进程结束"
return $!
}
careCurrentPrice()
{
local num=`count`
if [ $num -lt 1 ];then
start
num=`count`
notify "【$(basename $CONFIG)】现价程序挂掉重启~" "${BIN} -f ${CONFIG} 进程数:$num"
fi;
}
carePika()
{
local num=`pikaCount`
if [ $num -lt 1 ];then
pikaStart
num=`pikaCount`
notify "pika挂掉重启~" "pika进程数:$num"
log "pika restart,进程数:$num"
fi;
}
careRedis()
{
local num=`redisCount`
if [ $num -lt 1 ];then
redisStart
num=`redisCount`
notify "redis挂掉重启~" "redis进程数:$num"
log "redis restart,进程数:$num"
fi;
}
# 操作
case $op in
care)
#carePika
#careRedis
careCurrentPrice
;;
start | begin)
start
log "start finish"
;;
stop | end)
stop
log "stop finish"
;;
restart | reload)
stop
start
log "restart finish"
;;
status)
#log "pika进程数:`pikaCount`"
log "redis进程数:`redisCount`"
log "${BIN} ${CONFIG}进程数:`count`"
;;
*)
echo $0" [start|stop|restart|status|care]"
;;
esac