-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrun.sh
executable file
·154 lines (144 loc) · 3.41 KB
/
run.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
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
#!/bin/bash
# QsMail Server
# 基于Swoole的邮件服务器
# 青石 <[email protected]> http://www.qs5.org
# php主程序文件名
script_name='init.php'
# pid文件 请和 php 内设置一致
run_pid_file='/var/run/qsmail_server.pid'
# 获取当前运行目录
init_dir="$(cd "$(dirname "$0")" && pwd )"
# 运行日志保存文件夹
run_log_path="${init_dir}/logs"
# 脚本运行目录完整文件名
script_path="${init_dir}/${script_name}"
# 脚本运行时间
date_str=`date +%Y%m%d`
# 日志保存文件名
log_file="${run_log_path}/${date_str}_$$.log"
# 判断是否运行中 并获取进程pid
is_run(){
# 先判断pid文件是否存在
if [ -f "${run_pid_file}" ]; then
# 判断进程是否存在
run_pid=`cat ${run_pid_file}`
if [ -d "/proc/${run_pid}" ]; then
return 1
fi
fi
# 否则返回0
return 0
}
# 启动脚本
cmd_isStart(){
# 先判断是否运行中
is_run
if [ "$?" = "1" ]; then
echo "is Runing, pid: ${run_pid}"
return
fi
nohup php "${script_path}" start ${log_file} > ${log_file} 2>&1 &
echo "[$$] Start Ok!"
}
# 运行状态
cmd_isStatus(){
# 先判断是否运行中
is_run
if [ "$?" = "1" ]; then
echo "is Runing, pid: ${run_pid}"
return 1
else
echo "is Not Run!"
return 0
fi
}
# 重新加载脚本
cmd_isReload(){
# 先判断是否运行中
is_run
if [ "$?" = "1" ]; then
kill -USR1 ${run_pid}
return 1
else
echo "is Not Run!"
return 0
fi
}
# 重新启动脚本
cmd_isRestart(){
# 停止脚本
cmd_isStop
# 启动脚本
cmd_isStart
}
# 结束进程
cmd_isStop(){
# 先判断是否运行中
is_run
if [ "$?" = "1" ]; then
kill -15 ${run_pid}
if [ "$?" = "0" ]; then
echo -e "stop ${run_pid}\c"
# 判断进程是否结束
while [ -d "/proc/${run_pid}" ]
do
echo -e ".\c"
sleep 0.1
done
echo "success"
return 1
else
echo "stop pid: ${run_pid} error."
return 0
fi
else
echo "is Not Run!"
return 1
fi
}
# 强制杀死进程 不建议
cmd_isKill(){
# 先判断是否运行中
is_run
if [ "$?" = "1" ]; then
kill -9 ${run_pid}
if [ "$?" = "0" ]; then
echo -e "kill ${run_pid}\c"
# 判断进程是否结束
while [ -d "/proc/${run_pid}" ]
do
echo -e ".\c"
sleep 0.1
done
echo "success"
return 1
else
echo "kill pid: ${run_pid} error."
return 0
fi
else
echo "is Not Run!"
return 1
fi
}
# 判断日志文件夹是否存在
if [ ! -d "${run_log_path}" ]; then
mkdir ${run_log_path}
fi
# 判断命令
case "$1" in
"start") cmd_isStart ;;
"status") cmd_isStatus ;;
"reload") cmd_isReload ;;
"restart") cmd_isRestart ;;
"stop") cmd_isStop ;;
"kill") cmd_isKill ;;
*)
echo "+-------------------------------+"
echo "| QsMail Server Manager |"
echo "+-------------------------------+"
echo "| http://www.qs5.org |"
echo "+-------------------------------+"
echo "Usage: $0 {start|status|restart|reload|stop|kill}"
;;
esac