fabric执行在后台运行的命令

笑遍世界 2015-07-23 14:03:12 累计浏览 1,974 次

   最近一个项目需要用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.py

View Code PYTHON
1
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 <smile665@gmail.com>
 
from fabric.context_managers import cd
from fabric.operations import run, put
from fabric.api import task, env
 
env.hosts = ['192.168.1.2', '192.168.1.3', '192.168.1.4']
env.port = 22
env.user = 'root'
env.password = '123456'
 
 
@task
def hostname():
    # show hostname   # just for testing
    with cd('/tmp'):
        run('hostname')
 
 
@task
def copy_jmeter():
    # copy jmeter to other machines
    with cd('/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/')
 
 
@task
def start_jmeter():
    # run jmeter in all test clients
    #with cd('/tmp/'):
    with cd('/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)
 
 
@task
def kill_jmeter():
    # kill the jmeter processes for unified order project
    with cd('/tmp/'):
        pids = run("ps -ef | grep unified | grep -v 'grep' | awk '{print $2'}")
        pid_list = pids.split('\r\n')
        for i in pid_list:
            run('kill -9 %s' % i)
 
 
@task
def get_status():
    # get jmeter(java) running status
    with cd('/tmp'):
        run('ps -ef | grep unified | grep java | grep -v grep')

   参考文档:http://fabric.readthedocs.org/en/1.6/faq.html

同分类推荐文章

  1. NGINX Gateway Fabric 2.8: IP allow and deny lists, passive health checks, and cookie-based session persistence (2026-11-14 01:10:02)
  2. NGINX Ingress Controller 5.7: HTTP/2 to your upstreams, Opaque Secrets, and cert-manager challenges behind HTTPS redirects (2026-11-14 00:10:02)
  3. Hexo 文章一键发布到微信公众号 (2026-11-13 19:10:02)

查看更多 DevOps 文章 →

建议继续学习

  1. 用Hyer来进行网站的抓取 (累计阅读 158,836)
  2. 配置Nginx+uwsgi更方便地部署python应用 (累计阅读 109,256)
  3. 程序员技术练级攻略 (累计阅读 36,061)
  4. python实现自动登录discuz论坛 (累计阅读 33,280)
  5. python编程细节──遍历dict的两种方法比较 (累计阅读 21,007)
  6. WEB系统需要关注的一些点 (累计阅读 19,283)
  7. 每个程序员都应该学习使用Python或Ruby (累计阅读 18,309)
  8. Chrome和goagent的配置方法,你懂的 (累计阅读 17,490)
  9. 批量添加主机到cacti+nagios的监控报警系统中 (累计阅读 15,537)
  10. 30分钟3300%性能提升――python+memcached网页优化小记 (累计阅读 14,112)