功能说明:

  • 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 实战 -- 基于一个服务启动,关闭,状态检查的脚本的更多相关文章

  1. Linux下一个Redis启动/关闭/重新启动服务脚本

    脚本功能: 实现redis单机多实例情况下的正常启动.关闭.重新启动单个redis实例.完毕系统标准服务的下面经常使用功能:  start|stop|status|restart 注:redis程序代 ...

  2. Oracle 服务启动关闭

    有没有感觉到,自从装上Oracle之后,电脑变卡了?卡是正常的,oracle作为大型数据库软件,运行起来是非常吃内存的. 所以对于自用的电脑来说,不使用Oracle的时候,应该把Oracle的一系列服 ...

  3. Oracle 11g 服务启动/关闭 及 DB dump 导入

    本地启动Oracle 服务脚本 由于本地机子安装了Oracle后,会自动启动一些默认的Oracle服务,这样子会导致机子比较慢.所以需要改成手动启动/关闭服务. 即用即开,不用就关. 开启的脚本: O ...

  4. 简述uwsgi使用一个服务启动多个文件的用法

    [场景篇] 为了节省端口的占用,将N个flask应用服务——每个对应一个文件(web.py.django也一样)合并为一个端口服务来启用 [寻思篇] 通常的做法:每个文件配置一个xml 或者 ini文 ...

  5. linux 服务 启动 关闭 列表

    ##查看服务在每个级别的运行状态 chkconfig --list httpd           0:关闭  1:关闭  2:关闭  3:关闭  4:关闭  5:启用  6:关闭 bluetooth ...

  6. redis服务启动关闭脚本

    代码如下: # chkconfig: 90 10 # description: service of redis for start and stop add by tomener PATH=/usr ...

  7. Windows下快速启动/关闭orcl服务

    大家都知道windows下绝大部分都是图形操作化,很少用命令来执行,例如启动.关闭orcl数据库服务时,一般情况都是在任务管理器(taskmgr.ctrl+shift+esc)或服务(services ...

  8. linux学习一个服务(未完)

    学习一个服务的过程 1.了解服务的作用:名字 功能,特点 2.安装 3.配置文件位置,端口 4.服务启动关闭的脚本 5.此服务的使用方法 6.修改配置文件,实战举例 7.排错(从下到上,从内到外)

  9. Mongodb启动&关闭

    mac 下mongo的启动和关闭以及启动问题解决 mongo的安装在这:http://www.cnblogs.com/leinov/p/6855784.html Mac os mongodb数据安装路 ...

随机推荐

  1. c++ 标准 字符串转换为时间 时间大小比较 判断有效期 简洁办法

    c# php delphi java 等各种语言 对字符串转换为日期 然后与当前日期进行比较 是非常容易的 因为有现成的函数可用 标准 c++ 硬是找不到 合适的代码可用 于是 百度了很多 没百出个结 ...

  2. python+opencv抠图并旋转(根据坐标抠图)

    import cv2 import numpy as np def subimage(image, center, theta, width, height): theta *= np.pi / 18 ...

  3. openresty开发系列1--网关API架构及选型

    微服务架构在项目中的应用越来越多,我们知道在微服务架构风格中,一个大应用被拆分成为了多个小的服务系统提供出来,这些小的系统他们可以自成体系,也就是说这些小系统可以拥有自己的数据库,框架甚至语言等,这些 ...

  4. shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中

    shell编程系列24--shell操作数据库实战之利用shell脚本将文本数据导入到mysql中 利用shell脚本将文本数据导入到mysql中 需求1:处理文本中的数据,将文本中的数据插入到mys ...

  5. Opencv拉普拉斯算子做图像增强

    Opencv拉普拉斯算子——图像增强 #include <iostream> #include <opencv2/opencv.hpp> using namespace std ...

  6. cookie加载不正确的问题

    华为系统更新后安装了一个谷歌6月安全补丁的东西,然后之前写的调h5页面的部分就出现了问题,后台查过发现是Android端调h5页面时cookie没能带过去,导致了登录失败.于是对setCookie部分 ...

  7. 123457123456#2#----com.MCgame.ShuXueKoSuan98--前拼后广--儿童小学数学口算Game-mc22222

    com.MCgame.ShuXueKoSuan98--前拼后广--儿童小学数学口算Game-mc

  8. 如何在Windows Server 2008 R2中更改桌面图标

    如何在Windows Server 2008 R2中更改桌面图标 Windows Server 2008 R2 已经在 MSDN 和 TechNet Plus 订阅上公布,gOxiA 在第一时间下载并 ...

  9. Nginx配置文件的反向代理

    问题描述:项目需要预览pdf,前端控件支持的格式是http://192.168.0.1/pdf/a.pdf  是这样的,然后我就想给路径配个nginx反向代理就好了,但是配置的时候出问题了. 1.正确 ...

  10. IOPS 测试工具 FIO

    FIO是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎. fio-2.8下载: wget http://brick.kernel.dk/snaps/fio-2.8 ...