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
 
#!/bin/bash
 
### BEGIN INIT INFO
# Provides: 应用程序开发者
# RequiRED-Start: $all
# RequiRED-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: 简短应用程序描述
# Description: 详细应用程序描述
### END INIT INFO
 
# 程序文件目录
ROOT="/work/code/cusflo-server-moon"
# 程序文件
APP="./dog/client_update_dog.py"
# 程序配置文件
CONFIG="./server_config.ini"
# 服务日志
DEFAULT_LOG_FILE='./daodaoliang.log'
 
# 样式定制
RED="\\e[31m"
GREEN="\\e[32m"
YELLOW="\\e[33m"
BLACK="\\e[0m"
POS="\\e[60G"
 
ok_msg() {
echo -e "${1}${POS}${BLACK}[${GREEN} OK ${BLACK}]"
}
 
failed_msg() {
echo -e "${1}${POS}${BLACK}[${RED}FAILED${BLACK}]"
}
 
# load process info of app
# exist return 0
# no exist return 1
load_process_info() {
# get pid
app_pid=$(ps -ef|grep ${APP}|gawk '$0 !~/grep/ {print $2}' |tr -s '\n' ' ')
# check pid again
ps -p ${app_pid} >/dev/null 2>/dev/null
ret=$?; if [[ 0 -ne $ret ]]; then error_msg="process $app_pid does not exists"; return 1; fi
return 0;
}
 
start() {
# if exists, exit.
load_process_info
if [[ 0 -eq $? ]]; then failed_msg "${APP} started(pid ${app_pid}), should not start it again."; return 0; fi
 
# not exists, start server
ok_msg "Starting ${APP}..."
 
# get log file
log_file=`cd ${ROOT} && cat ${CONFIG} |grep '^log_file'| awk '{print $2}'| awk -F ';' '{print $1}'`
if [[ -z $log_file ]]; then log_file=${DEFAULT_LOG_FILE}; fi
# get abs path
log_dir=`dirname $log_file`
log_file=`(cd ${ROOT} && cd $log_dir && pwd)`/`basename $log_file`
 
# TODO: FIXME: set limit by, for instance, "ulimit -HSn 10000"
if [[ -z $log_file ]]; then
(cd ${ROOT};nohup python ${APP} >/dev/null 2>&1 &)
else
(cd ${ROOT};nohup python ${APP} >/dev.null >> $log_file 2>&1 &)
fi
 
# check again after start server
for ((i = 0; i < 5; i++)); do
# sleep a little while, for app may start then crash.
sleep 0.1
load_process_info
ret=$?; if [[ 0 -ne $ret ]]; then
failed_msg "${APP} start failed";
failed_msg "see $log_file";
return $ret;
fi
done
 
# check whether started.
load_process_info
ret=$?; if [[ 0 -eq $? ]]; then ok_msg "${APP} started(pid ${app_pid})"; return 0; fi
 
failed_msg "${APP} not started"
return $ret
}
 
stop() {
# not start, exit
load_process_info
if [[ 0 -ne $? ]]; then failed_msg "${APP} not start."; return 0; fi
 
ok_msg "Stopping ${APP}(pid ${app_pid})..."
 
# process exists, try to kill to stop normally
for((i=0;i<30;i++)); do
load_process_info
if [[ 0 -eq $? ]]; then
kill -s SIGTERM ${app_pid} 2>/dev/null
ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "send signal SIGTERM failed ret=$ret"; return $ret; fi
sleep 0.1
else
ok_msg "${APP} stopped by SIGTERM"
# delete the pid file when stop success.
rm -f ${pid_file}
break;
fi
done
 
# process exists, use kill -9 to force to exit
load_process_info
if [[ 0 -eq $? ]]; then
kill -s SIGKILL ${app_pid} 2>/dev/null
ret=$?; if [[ 0 -ne $ret ]]; then failed_msg "send signal SIGKILL failed ret=$ret"; return $ret; fi
ok_msg "${APP} stopped by SIGKILL"
else
# delete the pid file when stop success.
rm -f ${pid_file}
fi
 
sleep 0.1
return 0
}
 
# get the status of srs process
# @return 0 if srs is running; otherwise, 1 for stopped.
status() {
load_process_info
ret=$?; if [[ 0 -eq $ret ]]; then echo "${APP}(pid ${app_pid}) is running."; return 0; fi
 
echo "${APP} is stopped, $error_msg"
return 1
}
 
menu() {
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: $0 {start|stop|status|restart}"
return 1
;;
esac
}
 
menu $1
 
code=$?
exit ${code}
 
 
 
 
http://daodaoliang.com/linux%E5%AD%A6%E4%B9%A0/2015/02/04/linux%E5%AD%A6%E4%B9%A0-2015-02-04-%E6%9C%8D%E5%8A%A1%E8%84%9A%E6%9C%AC%E7%BC%96%E5%86%99/

linux服务脚本编写的更多相关文章

  1. shell编程之服务脚本编写,文件锁以及信号捕获

    shell脚本编程是linux运维工程师必备的技能,也是非常重要的一个技能,所以把shell编程学好,只有好处.基础语法我也就不讲了,学过C语言这些语言的,稍微看一下就能明白shell编程的基础,所以 ...

  2. Linux 用脚本编写搭建yum本地仓库

    Linux 用脚本编写搭建yum本地仓库 源码如下: #!/bin/bash #该脚本用于自动化搭建本地yum仓库 #挂载光盘 #作者:雨中落叶 #博客:https://www.cnblogs.com ...

  3. nginx 服务脚本编写模板

    编写nginx服务脚本:脚本内容如下: [root@www ~]# cat /etc/init.d/nginx #!/bin/bash # nginx Startup script for the N ...

  4. linux base脚本编写-自动领取微信红包

    bash脚本编写 语法 变量 定义: your_name = "ABC" 使用: echo $your_name 只读变量 a = "123" readonly ...

  5. linux服务脚本

    #!/bin/sh ARG=$1 case $ARG in start): nohup /path/program & ;; stop): pkill program ;; restart): ...

  6. 10个实战及面试常用Linux Shell脚本编写

    来自:http://blog.51cto.com/lizhenliang/1929044 注意事项 1)开头加解释器:#!/bin/bash 2)语法缩进,使用四个空格:多加注释说明. 3)命名建议规 ...

  7. linux Shell 脚本编写

    1. http://www.jb51.net/article/28514.htm 2. http://www.runoob.com/linux/linux-shell.html

  8. 简单的linux service(linux服务)编写,运行示例

    1.写一个简单小程序 #include<stdio.h> #include<stdlib.h> int main(int argc,char **argv) { while(1 ...

  9. linux 服务脚本

    #!/bin/bash # # chkconfig: # description: my_SERVICE_NAME is a my Service # # common function . /etc ...

随机推荐

  1. MySQL 基础查询

    6月业绩榜 名次   伙伴 业绩 签单 面谈 每日目标 1 杜艳花 12367 2 0 查看目标 2 郑东杰 2345 1 0 查看目标 3 黄传龙 345 1 1 查看目标 4 測试 0 0 0 查 ...

  2. 嵌入式project师考试知识点总结 微内核结构

    在操作系统的结构设计中,微内核结构表示的是 A)  总体式结构 B)  层次结构 C)  分布式结构 D)  客户机/server结构    微内核是指把操作系统结构中的内存管理.设备管理.文件系统等 ...

  3. 利用QPainter绘制各种图形(Shape, Pen 宽带,颜色,风格,Cap,Join,刷子)

    利用QPainter绘制各种图形 Qt的二维图形引擎是基于QPainter类的.QPainter既可以绘制几何形状(点.线.矩形.椭圆.弧形.弦形.饼状图.多边形和贝塞尔曲线),也可以绘制像素映射.图 ...

  4. MySql批量drop table

    原文:MySql批量drop table 今天发现数据库中很多没用的表,想清理掉. 发现mysql好像不支持类似这样的写法:drop table like "%r" 在oracle ...

  5. [Angular] @ContentChild and ngAfterContentInit

    @ContentChild normally works with ngAfterContentInit lifecycle. @ContentChild is used for looking in ...

  6. gdal以GA_Update方式打开jpg文件的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 gdal库是不支持以GA_Update方式打开jpg文件的,原因在于gdal_1_10_1\frmts\jpeg文件夹 ...

  7. 常规容器下SpringBootServletInitializer如何实现web.xml作用解析

    在之前的<使用jsp作为视图模板&常规部署>章节有过一个实践,需要启动类继承自SpringBootServletInitializer方可正常部署至常规tomcat下,其主要能够起 ...

  8. lucene 7.x 排序

    一.创建索引 @Test public void indexCreate() throws IOException { //创建分词器 Analyzer analyzer = new Standard ...

  9. dzone Cloud Zone

    dzone Cloud Zonehttps://dzone.com/cloud-computing-tutorials-tools-news有一些统计:https://dzone.com/refcar ...

  10. imageNamed 与 imageWithContentsOfFile差额

    郝萌主倾心贡献,尊重作者的劳动成果,请勿转载. 假设文章对您有所帮助.欢迎给作者捐赠,支持郝萌主,捐赠数额任意.重在心意^_^ 我要捐赠: 点击捐赠 Cocos2d-X源代码下载:点我传送 普通情况下 ...