shell 实战 -- 基于一个服务启动,关闭,状态检查的脚本
功能说明:
- check:检查服务状态,在开启,关闭,状态检查时都会用到这个函数,所以封装起来放到最前面
- start:开启服务
- stop:关闭服务
- fstop:强制关闭
- status:检查服务状态
- running:查看所有正在运行的状态
使用说明:
开启服务:
启动一个进程监听 9000(默认)端口:
- sh standard_server.sh start
启动一个进程监听 8000(自定义)端口:
- 格式: sh standard_server.sh start ipaddress port
- sh standard_server.sh start 10.10.4.200 8000
关闭服务:
关闭监听 9000(默认)端口:
- sh standard_server.sh stop
关闭监听 8000(自定义)端口:
- 格式: sh standard_server.sh stop ipaddress port
- sh standard_server.sh stop 10.10.4.200 8000
查看监听状态:
查看 9000 端口(默认)监听状态:
- sh standard_server.sh status
查看 8000端口(自定义)监听状态:
- 格式: sh standard_server.sh status ipaddress port
- sh standard_server.sh status 10.10.4.200 8000
查看所有正在监听端口:
- sh standard_server.sh running
代码部分:
#!/bin/bash
# author: ck check()
{
if (($(ps aux|grep manage.py| grep "${port:-9000}" |grep -v grep|wc -l) == 0));then
# stopped
return 1;
else
# running
return 0;
fi
} start()
{
check
if (($? == 1));then
echo -n "standard server ${port:-9000} to start......"
path=$(dirname $0)
if [[ $path != '.' ]];then
cd $path
fi
# nohup /home/seemmo/share/python/centos/python3.6.6/bin/python3 manage.py start_server -h "${host:-0.0.0.0}" -p "${port:-9000}" -k wf >nohup.out 2>&1 &
nohup python3 manage.py start_server -h "${host:-0.0.0.0}" -p "${port:-9000}" -k wf >nohup.out 2>&1 &
while true
do
check
if (($? == 1));then
echo -n '...'
sleep 1
else
echo -e '\033[32mstarted\033[1m\033[0m'
break
fi
done
else
echo "standard server ${port:-9000} has been running!!!"
fi
} fstop()
{
check
if (($? == 1));then
echo "standard server ${port:-9000} has been stopped!!!"
else
echo -n "standard server ${port:-9000} force to stop....."
ps aux|grep manage.py |grep "${port:-9000}" |grep -v grep|awk '{print $2}'|xargs kill -9
while true
do
check
if (($? == 1));then
echo -e '\033[32mstopped\033[1m\033[0m'
break
else
echo -n '...'
sleep 1
fi
done
fi
} stop()
{
check
if (($? == 1));then
echo "standard server ${port:-9000} has been stopped!!!"
else
echo -n "standard server ${port:-9000} to stop....."
spid=1
tp_list=($(ps aux|grep manage.py |grep "${port:-9000}" |grep -v grep|awk '{print $2}'|xargs))
for tpid in ${tp_list[@]}
do
if ((spid == 1));then
spid=$tpid
elif ((tpid < spid));then
spid=$tpid
fi
done
kill -15 $spid
retry_time=3
while true
do
if ((retry_time == 0));then
echo
fstop
break
fi
check
if (($? == 1));then
echo -e '\033[32mstopped\033[1m\033[0m'
break
else
((retry_time=retry_time-1))
echo -n '.'
sleep 1
fi
done
fi
} status()
{
check
if (($? == 1));then
echo -e "standard server ${port:-9000} now is \033[32mstopped\033[1m\033[0m"
else
echo -e "standard server ${port:-9000} now is \033[32mrunning\033[1m\033[0m"
fi
} running()
{
port_list=$(ps aux | grep "manage.py" | grep -v "grep" |awk '{print $17}' |xargs)
for port in ${port_list}
do
echo -e "standard server ${port} now is \033[32mrunning\033[1m\033[0m"
done
} restart() {
check
if (($? == 1));then
start
else
stop
while true
do
check
if (($? == 1));then
start
break
else
sleep 1
fi
done
fi
} if (($# == 1)) || (($# == 3));then
if (($# == 3));then
host=$2
port=$3
fi case $1 in
start|stop|status|restart|fstop|running)
$1
;;
*)
if (($# == 1));then
echo "Usage: bash $0 {start|stop|status|restart|fstop|running}"
exit 2
else
echo "Usage: bash $0 {start|stop|status|restart|fstop|running} host port"
exit 2
fi
esac else
echo "Usage: bash $0 {start|stop|status|restart|fstop|running} host port"
exit 2
fi
ending ~
shell 实战 -- 基于一个服务启动,关闭,状态检查的脚本的更多相关文章
- Linux下一个Redis启动/关闭/重新启动服务脚本
脚本功能: 实现redis单机多实例情况下的正常启动.关闭.重新启动单个redis实例.完毕系统标准服务的下面经常使用功能: start|stop|status|restart 注:redis程序代 ...
- Oracle 服务启动关闭
有没有感觉到,自从装上Oracle之后,电脑变卡了?卡是正常的,oracle作为大型数据库软件,运行起来是非常吃内存的. 所以对于自用的电脑来说,不使用Oracle的时候,应该把Oracle的一系列服 ...
- Oracle 11g 服务启动/关闭 及 DB dump 导入
本地启动Oracle 服务脚本 由于本地机子安装了Oracle后,会自动启动一些默认的Oracle服务,这样子会导致机子比较慢.所以需要改成手动启动/关闭服务. 即用即开,不用就关. 开启的脚本: O ...
- 简述uwsgi使用一个服务启动多个文件的用法
[场景篇] 为了节省端口的占用,将N个flask应用服务——每个对应一个文件(web.py.django也一样)合并为一个端口服务来启用 [寻思篇] 通常的做法:每个文件配置一个xml 或者 ini文 ...
- linux 服务 启动 关闭 列表
##查看服务在每个级别的运行状态 chkconfig --list httpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:启用 6:关闭 bluetooth ...
- redis服务启动关闭脚本
代码如下: # chkconfig: 90 10 # description: service of redis for start and stop add by tomener PATH=/usr ...
- Windows下快速启动/关闭orcl服务
大家都知道windows下绝大部分都是图形操作化,很少用命令来执行,例如启动.关闭orcl数据库服务时,一般情况都是在任务管理器(taskmgr.ctrl+shift+esc)或服务(services ...
- linux学习一个服务(未完)
学习一个服务的过程 1.了解服务的作用:名字 功能,特点 2.安装 3.配置文件位置,端口 4.服务启动关闭的脚本 5.此服务的使用方法 6.修改配置文件,实战举例 7.排错(从下到上,从内到外)
- Mongodb启动&关闭
mac 下mongo的启动和关闭以及启动问题解决 mongo的安装在这:http://www.cnblogs.com/leinov/p/6855784.html Mac os mongodb数据安装路 ...
随机推荐
- Flutter TextField详解
原文地址:https://www.jianshu.com/p/54419a143d70 实现TextField说简单也简单,说有坑,坑也不小,下面从易到难介绍一下使用 1.最简单的就是无参数调用构造方 ...
- pcm、wav、amr说明
wav比pcm多44个字节(在文件头位置多)
- 【笔记】第六章、Linux 的文件权限与目录配置
Linux的每个文件中,依据权限分为使用者.群组与其他人三种身份: 群组最有用的功能之一,就是当你在团队开发资源的时候,且每个账号都可以有多个群组的支持: 利用ls -l显示的文件属性中,第一个字段是 ...
- Linux记录-shell 100例(转载)
1.编写hello world脚本 #!/bin/bash # 编写hello world脚本 echo "Hello World!" 2.通过位置变量创建 Linux 系统账户及 ...
- Flink 物理分区
本文来自官网: https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/stream/operators/#physical-p ...
- Nginx - upstream sent invalid chunked response while reading upstream 异常问题
一个 post 的请求,直接调接口服务数据正常返回,但是通过 nginx 代理后, 什么都没有返回. nginx 配置如下: 使用 postman 调用,返回如下: 于是检查日志报错信息,如下: ng ...
- adb(Andorid Debug Bridge)安装和使用
以下是adb工具包最新2017Google官方版下载地址: ADB和Fastboot for Windows https://dl.google.com/android/repository/plat ...
- Django 高级视图
一.Django限制请求method 常用的请求method: GET请求:GET请求一般用来向服务器索取数据,但不会向服务器提交数据,不会对服务器的状态进行更改.比如向服务器获取某篇文章的详情. P ...
- PHP,Excel导出换行
// 有id,才算真的有发票数据 if ($v['b_invoice_id']) { $v['b_invoice_info'] = json_decode($v['b_invoice_json'],t ...
- 怎么对10亿数据量级的mongoDB作高效的全表扫描
转自:http://quentinxxz.iteye.com/blog/2149440 一.正常情况下,不应该有这种需求 首先,大家应该有个概念,标题中的这个问题,在大多情况下是一个伪命题,不应该被提 ...