IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    fabric执行在后台运行的命令

    master发表于 2015-07-06 20:43:36
    love 0
    最近一个项目需要用30个左右的jmeter客户端来做压力测试,需要做一些部署和配置工作,这时我喜欢的Fabric就可以发挥它的力量了。其中,我希望将jmeter施压段放到后台去运行,最开始将“nohup xxx &”放到fabric的run()中执行时,却没有执行;后来看了下文档,并做了些实验。对于直接使用“&”放到后台执行的情况,Fabric推荐使用如下3种情况来替代(按鲁棒性从高到低排列):1.使用已经存在daemon技术,如init、supervisord、upstart、systemd等,例如:/etc/init.d/tomcat restart 这样的命令,另外supervisord我也是很喜欢的。2.使用screen、tmux、dtach等工具来从当前shell中detach进程。如:screen -d -m sleep 10 这样的命令。注:要设置pty=False,如 run(‘screen -d -m sleep 10′, pty=False)。3.使用nohup,不过确实如官方文档所说只有部分用户能成功,我用nohup在run()中时就遇到了问题后该用screen了。共享一个前几天写的jmeter相关的fabfile吧:https://github.com/smilejay/python/blob/master/py2015/unified_order_fabfile.pyView CodePYTHON1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58#!/usr/bin/env python# a fabfile to manange the performance test for unified order project.# usage: fab -f unified_order_fabfile.py start_jmeter -P -z 30# author: Jay fromfabric.context_managersimportcdfromfabric.operationsimportrun, putfromfabric.apiimporttask, env env.hosts=['192.168.1.2','192.168.1.3','192.168.1.4']env.port=22env.user='root'env.password='123456' @taskdefhostname():# show hostname # just for testingwithcd('/tmp'): run('hostname') @taskdefcopy_jmeter():# copy jmeter to other machineswithcd('/tmp'): run('rm -rf jakarta-jmeter-2.3.4')put('jakarta-jmeter-2.3.4','/tmp/')run('cd jakarta-jmeter-2.3.4/bin; chmod a+x jmeter')#run('ls /tmp/') @taskdefstart_jmeter():# run jmeter in all test clients#with cd('/tmp/'):withcd('/tmp/jakarta-jmeter-2.3.4/bin/'): run('screen -d -m ./jmeter -n -t my-order.jmx -l log.jtl &>abc.log')#run('./jmeter -n -t unified-order.jmx -l log.jtl &>abc.log')#run('screen -d -m sleep 10', pty=False)#run('service tomcat start', pty=False) @taskdefkill_jmeter():# kill the jmeter processes for unified order projectwithcd('/tmp/'): pids = run("ps -ef | grep unified | grep -v 'grep' | awk '{print $2'}")pid_list = pids.split('\r\n')foriinpid_list: run('kill -9 %s'%i) @taskdefget_status():# get jmeter(java) running statuswithcd('/tmp'): run('ps -ef | grep unified | grep java | grep -v grep')参考文档:http://fabric.readthedocs.org/en/1.6/faq.htmlOriginal article:fabric执行在后台运行的命令©2015笑遍世界. All Rights Reserved.


沪ICP备19023445号-2号
友情链接