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数据安装路 ...
随机推荐
- 搭建 Kafka 集群 (v2.12-2.3.0)
服务器:10.20.32.121,10.20.32.122,10.20.32.123 三台服务器都需要安装jdk.配置zookeeper.配置kafka 1.安装配置jdk1.8 [root@loca ...
- python的xpinyin模块:汉字转拼音
pypinyin 1.安装 pip install pypinyin 2.使用方法 >>> from pypinyin import ...
- Summary: curated List of Top 75 LeetCode Questions to Save Your Time
Facebook / Eng tech lead Dec 30, 2018 68 Comments New Year Gift to every fellow time-constrained ...
- java IO 文件批量重命名
java IO 文件批量重命名 package com.vfsd.renamefile; import java.io.File; import java.io.FileInputStream; im ...
- 译文:A Robust and Modular Multi-Sensor Fusion ApproachApplied to MAV Navigation
A Robust and Modular Multi-Sensor Fusion ApproachApplied to MAV Navigation 众所周知,将来自多个传感器的信息融合用于机器人导航 ...
- Dijkstra算法(朴素实现、优先队列优化)
Dijkstra算法只能求取边的权重为非负的图的最短路径,而Bellman-Ford算法可以求取边的权重为负的图的最短路径(但Bellman-Ford算法在图中存在负环的情况下,最短路径是不存在的(负 ...
- RBF神经网络学习算法及与多层感知器的比较
对于RBF神经网络的原理已经在我的博文<机器学习之径向基神经网络(RBF NN)>中介绍过,这里不再重复.今天要介绍的是常用的RBF神经网络学习算法及RBF神经网络与多层感知器网络的对比. ...
- NMF包的安装
win10 操作系统,R3.4版本 NMF包的下载 source("https://bioconductor.org/biocLite.R") biocLite("Bio ...
- TCP/IP学习笔记7--TCP/IP模型通信例子学习
"一位如蝴蝶般美丽的女子向我飞来,翩翩的舞姿如同云端轻盈的叶儿." -------------------------------------------------------- ...
- Spring @Transactional 事务机制
几个概念要清楚:事务的传播机制,事务的边界 工作原理 运行配置@Transactional注解的测试类的时候,具体会发生如下步骤 1)事务开始时,通过AOP机制,生成一个代理connection对象, ...