nginx启动脚本(class练习)
说明:使用类的方式编写程序启动脚本(练习) 1 #!/usr/bin/env python import sys
import os
from subprocess import Popen,PIPE class Process(object): def __init__(self,name,pfile,workdir):
self.name = name
self.pfile = pfile
self.workdir = workdir
self._init() def _init(self):
if os.path.exists(self.workdir):
os.chdir(self.workdir)
else:
os.makedirs(self.workdir)
os.chdir(self.workdir) def _pidfile(self):
return os.path.join(self.workdir,self.pfile) def _writepid(self):
if self.pid:
with open(self.pfile,'w') as fd:
fd.write('%s' % self.pid) def _getpid(self):
p = Popen(['pidof','nginx'],stdout=PIPE)
return p.stdout.read() def start(self):
cmd = self.name
if self._getpid():
sys.exit('The %s are started!!' % self.name)
else:
p = Popen(cmd,stdout=PIPE,shell=True)
self.pid = (p.pid + 1)
self._writepid()
print('The %s is start ' % self.name) def stop(self):
if self._getpid():
for pid in self._getpid().strip().split():
os.kill(int(pid),15)
if os.path.exists(self._pidfile()):
os.remove(self._pidfile())
print('The %s is stopped... ' % self.name)
else:
print('The %s is not running...' % self.name) def restart(self):
self.stop()
self.start() def status(self):
if self._getpid():
print('The %s status is running...' % self.name)
else:
print('The %s status is stopped ...' % self.name) def reload(self):
os.system('%s -s reload' % self.name)
print('The %s is reload Successful !' % self.name ) def help(self):
print('Usage: %s {start|stop|restart|reload}' % __file__) def main():
name = '/usr/sbin/nginx'
workdir = '/var/run/nginx'
pfile = 'nginx.pid'
nginx = Process(name,pfile,workdir)
try:
para = sys.argv[1]
except IndexError as e:
sys.exit(e)
if para == 'start':
nginx.start()
elif para == 'stop':
nginx.stop()
elif para == 'restart':
nginx.restart()
elif para == 'status':
nginx.status()
elif para == 'reload':
nginx.reload()
else:
nginx.help() if __name__ == '__main__':
main()
nginx启动脚本(class练习)的更多相关文章
- linux nginx 启动脚本
linux nginx 启动脚本 [root@webtest76 ~]# vi /etc/init.d/nginx #!/bin/bash # nginx Startup script for the ...
- Nginx 启动脚本/重启脚本
第一步先运行命令关闭nginx sudo kill `cat /usr/local/nginx/logs/nginx.pid` 第二步 vi /etc/init.d/nginx 输入以下内容 #!/b ...
- nginx启动脚本,手动编辑
nginx启动脚本,手动编辑 #! /bin/bash # chkconfig: - # description: nginx service XDIR=/www/server/nginx DESC= ...
- centos LNMP第一部分环境搭建 LAMP LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/php/{p.conf.default,p.conf} php运行方式SAPI介绍 第二十三节课
centos LNMP第一部分环境搭建 LAMP安装先后顺序 LNMP安装先后顺序 php安装 安装nginx 编写nginx启动脚本 懒汉模式 mv /usr/local/php/{ ...
- Nginx 启动脚本
Nginx 启动脚本 1.vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # S ...
- LNMP 1.4 nginx启动脚本和配置文件
编写Nginx启动脚本,写入下面这段,授权755 vim /etc/init.d/nginx #!/bin/bash # chkconfig: - # description: http servic ...
- shell脚本之nginx启动脚本、统计日志字段、for循环实战、跳板机
1.NGINX启动脚本 #!/bin/bash # chkconfig: 235 32 62 # description: nginx [ -f /etc/init.d/functions ] &am ...
- nginx启动脚本和配置文件
1.编写Nginx启动脚本,并加入系统服务 vim /etc/init.d/nginx并在其中写入如下内容:#!/bin/bash# chkconfig: - 30 21# description: ...
- 开发nginx启动脚本及开机自启管理(case)
往往我们在工作中需要自行写一些脚本来管理服务,一旦服务异常或宕机等问题,脚本无法自行管理,当然我们可以写定时任务或将需要管理的脚本加入自启等方法来避免这种尴尬的事情,case适用与写启动脚本,下面给大 ...
随机推荐
- 用es6写一个分数库
es6发布后nodejs开始更新.最近写一些库发现新特性还是很好用的,于是回来写一个分数库练手. 对于es6本身 ... => 以及 array.includes 很简洁.class依然不是很顺 ...
- 「日常训练」「小专题·USACO」 Barn Repair(1-4)
题意 之后补. 分析 这题同样也很精巧.我们不妨思考一下,如果只允许用一块木板,那么要购买多少距离?是整个的距离吗?不是,是从第一个到最后一个(哈哈哈哈哈哈哈).但是,不包括第一个的"左边& ...
- Qt 加载Leap motion 手势识别软件 二次开发 hello world
研发需要对收拾是被进行精确定位,实现收拾的识别,和在虚拟现实中精确的显示手势在实际世界中的位置. 开始使用的Qt mingw的版本开发,总是函数没有定义,最后发现是leap sdk中需要代育vs的库文 ...
- Windows Server 2008 R2(x64) IIS7+PHP5(FastCGI)环境搭建
相关软件下载: 1.PHP下载地址: http://windows.php.net/downloads/releases/php-5.4.4-nts-Win32-VC9-x86.zip 如果是win2 ...
- 孤荷凌寒自学python第七十三天开始写Python的第一个爬虫3
孤荷凌寒自学python第七十三天开始写Python的第一个爬虫3 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 直接上代码.详细过程见文末屏幕录像 ...
- 文件名的查找——find
查找文件!!! 命令格式:find [PATH] [option] [action] 一.参数1——与时间有关的参数 -atime.-ctime.-mtime -mtime n:在n天之前的“一天之内 ...
- 关于socket的疑问
一直感觉一端发送数据,另一端接受数据很不可思议的事情,如果不能即时地读走会导致什么后果呢? 其实socket读出来的数据,你自己看着办,里面的数据是什么格式你自己去解析,用户可以基于TCP去实现你自己 ...
- [C/C++] C++声明和定义的区别
·变量定义:用于为变量分配存储空间,还可为变量指定初始值.程序中,变量有且仅有一个定义. ·变量声明:用于向程序表明变量的类型和名字. ·定义也是声明:当定义变量时我们声明了它的类型和名字. ·ext ...
- 【Python】Linux crontab定时任务配置方法(详解)
CRONTAB概念/介绍 crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行. cron 系统调度进程. 可以使用它在 ...
- [LOJ #2538][PKUWC 2018]Slay the Spire
题目大意:一开始有 $2n$ 张牌,每张牌上都写着一个数字 $w_i$,有两种的牌,每种类型各 $n$ 张: 1.攻击牌:打出后对对方造成牌上的数字的伤害. 2.强化牌:打出后,设数字为 $x$,则其 ...