说明:使用类的方式编写程序启动脚本(练习)

 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练习)的更多相关文章

  1. linux nginx 启动脚本

    linux nginx 启动脚本 [root@webtest76 ~]# vi /etc/init.d/nginx #!/bin/bash # nginx Startup script for the ...

  2. Nginx 启动脚本/重启脚本

    第一步先运行命令关闭nginx sudo kill `cat /usr/local/nginx/logs/nginx.pid` 第二步 vi /etc/init.d/nginx 输入以下内容 #!/b ...

  3. nginx启动脚本,手动编辑

    nginx启动脚本,手动编辑 #! /bin/bash # chkconfig: - # description: nginx service XDIR=/www/server/nginx DESC= ...

  4. 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/{ ...

  5. Nginx 启动脚本

    Nginx 启动脚本 1.vim /etc/init.d/nginx #!/bin/bash # chkconfig: - 30 21 # description: http service. # S ...

  6. LNMP 1.4 nginx启动脚本和配置文件

    编写Nginx启动脚本,写入下面这段,授权755 vim /etc/init.d/nginx #!/bin/bash # chkconfig: - # description: http servic ...

  7. shell脚本之nginx启动脚本、统计日志字段、for循环实战、跳板机

    1.NGINX启动脚本 #!/bin/bash # chkconfig: 235 32 62 # description: nginx [ -f /etc/init.d/functions ] &am ...

  8. nginx启动脚本和配置文件

    1.编写Nginx启动脚本,并加入系统服务 vim /etc/init.d/nginx并在其中写入如下内容:#!/bin/bash# chkconfig: - 30 21# description: ...

  9. 开发nginx启动脚本及开机自启管理(case)

    往往我们在工作中需要自行写一些脚本来管理服务,一旦服务异常或宕机等问题,脚本无法自行管理,当然我们可以写定时任务或将需要管理的脚本加入自启等方法来避免这种尴尬的事情,case适用与写启动脚本,下面给大 ...

随机推荐

  1. iOS笔记058 - IOS之多线程

    IOS开发中多线程 主线程 一个iOS程序运行后,默认会开启1条线程,称为"主线程"或"UI线程" 作用 显示和刷新界面 处理UI事件(点击.滚动.拖拽等) 注 ...

  2. Vue学习(二):class与style绑定

    <!DOCTYPE html> <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xhtml&q ...

  3. 【page.js】配置及Page函数说明

    页面.js中的Page函数用来注册一个页面,指定页面的初始数据.生命周期回调.事件处理函数等. 语法:Page(Object)参数: Object json对象 Page({ /** * data * ...

  4. LINQ学习笔记——(3)基本查询操作符

    Select() 作用于uIEnumerable<TSource>类型 public static void Test() { List<string> persons = n ...

  5. [leetcode-652-Find Duplicate Subtrees]

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only ne ...

  6. HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  7. JDK源码分析 – Integer

    Integer类的申明 public final class Integer extends Number implements Comparable<Integer> { … } Int ...

  8. OpenCV膨胀和腐蚀示例代码

    #include<cv.h> #include<highgui.h> int main(int argc, char** argv) { IplImage* img = cvL ...

  9. about !dbgprint to analyze BSOD dump file.

    基本规则: 只有debug mode enable的机器,产生的dump file才会保存dbgprint的buffer. 默认!dbgprint的buffer size是4k. 增加buffer s ...

  10. BZOJ4560 JLOI2016字符串覆盖(kmp+贪心+状压dp+单调队列)

    首先kmp求出每个子串能放在哪些位置.接下来的两部分贪心和状压都可以,各取比较方便的. 最大值考虑贪心.考虑枚举子串的左端点出现顺序,在此基础上每个子串的位置肯定都应该尽量靠前,有是否与上个子串有交两 ...