zoukankan      html  css  js  c++  java
  • 用python的paramiko批量杀掉jps进程后重启hadoop&spark集群

    hadoop集群关闭异常可能会在集群中残留jps下的进程影响后面的重启,下面通过python的paramiko模块批量杀死影响重启的进程

    import paramiko
    #建立连接,这里假定集群中的hadoop用户都用相同的账号密码
    def connect(host):
    'this is use the paramiko connect the host,return conn'
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    try:

    ssh.connect(host,username='zkpk',password='zkpk',allow_agent=True)
    return ssh
    except:
    return None

    #执行命令
    def exec_commands(conn,cmd):
    'this is use the conn to excute the cmd and return the results of excute the command'
    stdin,stdout,stderr = conn.exec_command(cmd)
    results=stdout.read()
    return results



    if __name__ == '__main__':
    #集群主机ip,逐个登陆主机执行jps命令查找进程id,然后干掉,如果集群主机较多可以通过并行的方式提高效率
    for ip in ['192.168.134.174','192.168.134.175','192.168.134.176']:
    a=exec_commands(connect(ip), '/usr/java/jdk1.7.0_71/bin/jps')
    ps=[]
    #字符串分析,提取进程id
    ls=a.split(' ')
    for l in ls:
    if len(l.split(' ')[0])>=1:
    ps.append(l.split(' ')[0])

    for psd in ps:
    a = exec_commands(connect(ip), 'kill -9 %s' % psd)
    print a



    代码比较粗糙,当然完全可以用更高封装的自动运维工具fabric做上述工作
  • 相关阅读:
    URL传参到servlet含特殊字符——#号无法传递
    textarea标签中间出现空格问题
    MySQL学习笔记(23)——自定义函数
    修改日期插件问题
    获取Spring的ApplicationContext的方法
    数据库用户被锁
    ava获得当前文件路径
    前端框架
    LeetCode 485. 最大连续1的个数
    LeetCode 283. 移动零
  • 原文地址:https://www.cnblogs.com/zyzloner/p/6557934.html
Copyright © 2011-2022 走看看