功能说明:

  • 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. javascript取模运算是怎么算的?其实是取余数

    问到是否整除,这里记录下取模 比如120分钟是不是整点?120%60 === 0 为整点 javascript取模运算是一个表达式的值除以另一个表达式的值,并返回余数. 取模在js里就是取余数的意思. ...

  2. Linux expect的安装与使用

    Expect是在Tcl的基础上创建的,可以用来做一些Linux下无法做到交互的命令操作,可用于远程管理服务器. 一.安装Tcl: 1.下载源码包: wget http://nchc.dl.source ...

  3. 【429】关于ADT的访问权限

    在看老师代码的时候,发现ADT中的 struct 有时候写到了 adt.c 里面,有时候写到了 adt.h 里面,其实有些困惑,经过仔细研究,发现写在 adt.h 中的 struct 可以在 test ...

  4. LeetCode_367. Valid Perfect Square

    367. Valid Perfect Square Easy Given a positive integer num, write a function which returns True if  ...

  5. WINDOWS配置WSUS。

    wsus的注册表文件! Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINESOFTWAREPoliciesMicrosoftWindows ...

  6. Elasticsearch - 处理冲突

    http://blog.csdn.net/xifeijian/article/details/49615559

  7. C# 需要引用MySql.Data.dll,请在Nuget安装最新稳定版本,如果有版本兼容问题请先删除原有引用 (SqlSugar)

    修改项目的app.config中的引用版本号即可

  8. 03.linux入门命令

    1.linux命令的格式 命令 [选项] [参数] eg: ls ls -l ls -l /home 注: a.选项与参数不一定存在 b.选项用 "-" 来指明 c.命令,选项,参 ...

  9. 在Ubuntu下安装qt

    安装qt前的准备: 安装基础开发包,主要gcc,g++等: sudo apt-get install build-essential 1. 使用qt-opensource-linux-x86-5.5. ...

  10. 024 Android 自定义样式对话框(AlertDialog)

    1.AlertDialog介绍 AlertDialog并不需要到布局文件中创建,而是在代码中通过构造器(AlertDialog.Builder)来构造标题.图标和按钮等内容的. 常规使用步骤(具体参见 ...