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()等) 实现功能:显 ...
随机推荐
- HYPER-V的安装和双机调试的配置(一)
在上一篇文章中,我们已经安装好了VS2017以及WDK,现在我们就需要创建双机调试的环境, 因为本人的工作环境问题,不能使用WMWARE进行虚拟机的安装,因此就针对HYPER-V这个的虚拟机来进行双机 ...
- springboot-27-整合mybatis,druid连接池
sprinboot整合mybatis, 有2种方式, 第一种完全使用注解的方式, 还有一种就是使用xml文件的方式 项目使用gradle + idea, 数据源使用druid, 多使用groovy编写 ...
- php里input数组的应用
一般我们使用input传递表单数据时,可以使用<input name="xxx[]" value="1"><input name=" ...
- [Python] 震惊, 我居然用Python干这种事ꈍ .̮ ꈍ
阅读本文只需花费你两分钟, 两分钟你买不了吃亏,你也买不了上当. 那么, 为何不静下心来看看呢? Python 海龟创意绘画, Turtle库创作精美图画 Author:Amd794 E-ma ...
- <Think Python>中斐波那契使用memo实现的章节
If you played with the fibonacci function from Section 6.7, you might have noticed thatthe bigger th ...
- intellij idea 怎么全局搜索--转
https://jingyan.baidu.com/article/29697b9163ac7dab20de3cbf.html intellij idea是一款智能,功能强大的ide,对比eclips ...
- 并发编程之 ThreadLocal 源码剖析
前言 首先看看 JDK 文档的描述: 该类提供了线程局部 (thread-local) 变量.这些变量不同于它们的普通对应物,因为访问某个变量(通过其 get 或 set 方法)的每个线程都有自己的局 ...
- MVC用非Entity Framework将数据显示于视图(二)
这篇<MVC用非Entity Framework将数据显示于视图> http://www.cnblogs.com/insus/p/3364235.html 也算是MVC视图显示数据库的数据 ...
- 无责任Windows Azure SDK .NET开发入门(二):使用Azure AD 进行身份验证
<編者按>本篇为系列文章,带领读者轻松进入Windows Azure SDK .NET开发平台.本文为第二篇,将教导读者使用Azure AD进行身分验证.也推荐读者阅读无责任Windows ...
- ubuntu 上安装ssh
1. 执行 sudo apt-get update 2. 安装 sudo apt-get install openssh-server 3.查看ssh服务状态 sudo service ssh sta ...