python编写脚本
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sys
import os
from subprocess import Popen,PIPE
class Process(object):
'''memcached process manger'''
def __init__(self,name,program,args,workdir):
self.name = name
self.program = program
self.args = args
self.workdir = workdir
def _init(self):
if not os.path.exists(self.workdir):
os.mkdir(self.workdir)
os.chdir(self.workdir)
def _pidFile(self):
return os.path.join(self.workdir,'%s.pid' %self.name) def _writePid(self):
if self.pid:
with open(self._pidFile(),'w') as fd:
fd.write(str(self.pid)) def start(self):
if self._getPid():
print 'memcache is alreday start!!!'
sys.exit()
self._init()
cmd = self.program + ' ' + self.args
p = Popen(cmd,stdout=PIPE,shell=True)
self.pid = p.pid
self._writePid()
print '%s start sucessful' %(self.name) def _getPid(self):
p = Popen(['pidof',self.name],stdout=PIPE)
pid = p.stdout.read().strip()
return pid def stop(self):
pid = self._getPid()
if pid:
os.kill(int(pid),15)
if os.path.exists(self._pidFile()):
os.remove(self._pidFile())
print '%s is stopped' %self.name
def restart(self):
self.stop()
self.start() def status(self):
pid = self._getPid()
if pid:
print '%s is already running' % self.name
else:
print '%s is stop' % self.name def help(self):
# print 'start|stop|status'
print 'Usage %s {start|stop|restart|status}' %__file__ def main():
name = 'memcached'
prog = '/usr/bin/memcached'
args = '-u nobody -p 11211 -c 1024 -m 64'
wd = '/var/tmp/memcached'
pm = Process(name,prog,args,wd)
try:
cmd = sys.argv[1]
except:
print 'Opiton Error'
sys.exit()
if cmd =='start':
pm._init()
pm.start()
elif cmd == 'stop':
pm.stop() elif cmd =='restart':
pm.restart()
elif cmd == 'status':
pm.status()
else:
pm.help() if __name__=='__main__':
main()
python编写脚本的更多相关文章
- 使用 Python 编写脚本并发布
使用 Python 编写脚本并发布 P1: 脚本 通常在 Linux 服务器上会遇到在命令行中输入命令的操作,而有些操作包含的命令数目较多或者其中的命令包含的参数较多,如果一个一个的敲命令的话就太麻烦 ...
- python编写脚本应用实例
这里主要记录工作中应用python编写脚本的实例.由于shell脚本操作数据库(增.删.改.查)并不是十分直观方便,故这里采用python监控mysql状态,然后将状态保存到数据库中,供前台页面进行调 ...
- 【MonkeyRunner】[技术博客]用python编写脚本查看设备信息
[MonkeyRunner]用python编写脚本查看设备信息 原以为是个简单的操作,在实践的时候发现了一些问题. python脚本 test.py: from com.android.monkeyr ...
- python编写脚本,删除固定用户下的所有表
脚本如下: [oracle@ycr python]$ more t_del.py #/usr/bin/python#coding:utf8 import sysimport cx_Oracle i=0 ...
- Zabbix调用外部脚本发送邮件:python编写脚本
Zabbix调用外部脚本发送邮件的时候,会在命令行传入两个参数,第一个参数就是要发送给哪个邮箱地址,第二个参数就是邮件信息,为了保证可以传入多个参数,所以假设有多个参数传入 #!/usr/bin/en ...
- Python编写脚本(输出三星形状的‘*’符号)
环境:python3.* 心得:个人认为脚本非我强项,以下效果可以有更简单解决方案,纯属练习逻辑. 方案一: s=1 while s<=10: #这是决定多少列,起始为1,大循环一圈即加一,就是 ...
- python编写脚本,登录Github通过指定仓库指定敏感关键字搜索自动化截图生成文件【完美截图】
前言:为了避免开发人员将敏感信息写入文件传到github,所以测试人员需要检查每个仓库是否有写入,人工搜索审核比较繁琐,所以写一个脚本通过配置 配置文件,指定需要搜索的仓库和每个仓库需要搜索的关键字, ...
- python编写网络抓包分析脚本
python编写网络抓包分析脚本 写网络抓包分析脚本,一个称手的sniffer工具是必不可少的,我习惯用Ethereal,简单,易用,基于winpcap的一个开源的软件 Ethereal自带许多协议的 ...
- python编写文件统计脚本
python编写文件统计脚本 思路:用os模块中的一些函数(os.listdir().os.path.isdir().os.path.join().os.path.abspath()等) 实现功能:显 ...
随机推荐
- 修改Hosts文件提示没有权限怎么办
解决办法:给host文件赋予权限 1.打开电脑C盘,在目录C:\Windows\System32\drivers\etc 下找到hosts文件 2.右键hosts文件,选择属性 3.点击hosts属性 ...
- js去除字符串中的标签
var str="<p>js去除字符串中的标签</p>"; var result=str.replace(/<.*?>/ig,"&qu ...
- 轻量级web富文本框——wangEditor使用手册(5)——配置“插入代码”功能
最新版wangEditor: demo.文档:http://www.wangEditor.github.io/ 下载地址:https://github.com/wangfupeng1988/wangE ...
- js便签笔记(12)——浏览TOM大叔博客的学习笔记 part2
1. 前言 昨天写了<js便签笔记(11)——浏览TOM大叔博客的学习笔记 part1>,简单记录了几个问题.part1的重点还是在于最后那个循环创建函数的问题,也就是多个子函数公用一个闭 ...
- Vue + Element UI 实现权限管理系统 前端篇(三):工具模块封装
封装 axios 模块 封装背景 使用axios发起一个请求是比较简单的事情,但是axios没有进行封装复用,项目越来越大,会引起越来越多的代码冗余,让代码变得越来越难维护.所以我们在这里先对 axi ...
- Spring框架引入
Struts与Hibernate可以做什么事? Struts, Mvc中控制层解决方案 可以进行请求数据自动封装.类型转换.文件上传.效验… Hibernate, 持久层的解决方案: 可以做到, 把对 ...
- C++类型转化小结
之前面试就有被问过关于使用类型转换的问题,因为主要是做Windows驱动的开发,一直都是纯C语言+汇编,当时真的是只用过C语言的强制类型转换,C语言的强制类型转换(Type Cast)很简单,不管什么 ...
- 数组filter()参数详解,巧用filter()数组去重
数组方法挺多,但是用来用去可能也就foreach,splice以及slice接触较多,filter()说实话之前也没过多了解.其实filter()为数组提供过滤功能,它会遍历数组所有元素,并返回满足条 ...
- Spring基础(3) : 静态工厂和实例工厂创建bean
public class Factory { public static Person staticCreate(){ Person p = new Person(); p.name="st ...
- MFC函数—CSingleDocTemplate
前提:在InitInstance() 函数的初始化过程中,我们可以看到代码CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDoc ...