通過python 執(zhí)行 nohup 不生效的解決
通過paramiko模塊ssh登錄linux,然后用exec_command方法執(zhí)行帶有nohup的shell命令不生效,python腳本如下:
import paramiko import time ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('192.168.1.2', 22, 'root', '123456') ssh.exec_command('nohup ping localhost & \n') time.sleep(1)
腳本執(zhí)行完之后ping進程并沒有繼續(xù)運行,這可能是因為exec_command執(zhí)行完之后立刻關閉通道的原因,換用invoke_shell可以正常運行:
import paramiko import time ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect('192.168.1.2', 22, 'root', '123456') chan = ssh.invoke_shell() chan.send('nohup ping localhost & \n') time.sleep(1)
注意,命令最后的回車\n和延時必不可少
補充知識:paramiko遠程服務器nohup阻塞問題
一、需求描述:
需要來回切換多臺服務器(腳本命令不太熟),就用了python的paramiko模塊進行遠程連接服務器,控制程序的停止和啟動。安裝:pip install paramiko
二、問題描述:
import paramiko # 創(chuàng)建SSH對象 ssh = paramiko.SSHClient() # 允許連接不在know_hosts文件中的主機 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 連接服務器 ssh.connect(hostname='192.168.0.3', port=22, username='xxx') # 執(zhí)行命令 stdin, stdout, stderr = ssh.exec_command('cd ~/ ; nohup python3.6 run_test.py > nohup_test.log 2>&1 &') # 獲取命令結果 result = stdout.read() # 關閉連接 ssh.close()
這樣連接服務器的時候確實可以執(zhí)行,但是遇到會阻塞的任務時,就無法生效,找了很多方法,最后發(fā)現這個比較有效。
三、解決方法
import paramiko # 創(chuàng)建SSH對象 ssh = paramiko.SSHClient() # 允許連接不在know_hosts文件中的主機 ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 連接服務器 ssh.connect(hostname='192.168.0.3', port=22, username='xxx', key=private_key) # 添加下面代碼 transport = ssh.get_transport() channel = transport.open_session() # 執(zhí)行命令 此方法沒有返回值 channel.exec_command('cd ~/ ; nohup python3.6 run_test.py > nohup_test.log 2>&1 &') # 關閉連接 ssh.close()
四、類的調用實現:
簡單測試,見下面代碼
# -*- coding: utf-8 -*- """ 20190330 """ import paramiko import time from confs.log import logger # 自行導入logging模塊即可 class EasyConnectHandle(object): """操作遠程服務器""" def __init__(self, connect_host_name:dict): """初始化參數""" """ "test":{ "ip":"192.168.0.189", "user_name":"xxxx", "pwd":"huhuhu" }, """ self.connect_host = connect_host_name self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # 允許連接陌生服務器 self.ssh.connect(hostname=self.connect_host["ip"], port=22, username=self.connect_host["user_name"], password=self.connect_host["pwd"], timeout=10) # 初始化的時候連接到新的服務器 logger.info(f"登錄服務器---{self.connect_host['ip']}成功:") def __new__(cls, *args, **kwargs): """單例模式""" if not hasattr(cls, '_instance'): cls._instance = super(EasyConnectHandle, cls).__new__(cls) return cls._instance def exec(self, cmd=""): """執(zhí)行操作""" stdin, stdout, stderr = self.ssh.exec_command(cmd) return stdout.read().decode() def quit(self): """斷開服務器""" self.ssh.close() logger.info(f"退出服務器---{self.connect_host['ip']}成功") if __name__ == '__main__': test_host = { "test": { "ip": "192.168.0.111", "user_name": "xxxx", "pwd": "xxxx", "jobs": [ { "path": "/home/lemon", "type": "touch test_1.sh" }, { "path": "/home/lemon", "type": "touch test_2.sh" } ] } } for i in ["test"]: easy_conn = EasyConnectHandle(test_host[i]) transport = easy_conn.ssh.get_transport() if len(test_host[i].get("jobs", [])) >= 1: for job in test_host[i]["jobs"]: channel = transport.open_session() channel.exec_command(f"cd {job['path']};{job['type']}") logger.info(f"服務器---{easy_conn.connect_host['ip']}執(zhí)行---cd {job['path']};{job['type']}---成功") time.sleep(2) else: logger.info(f"服務器---{easy_conn.connect_host['ip']}暫時沒有任務") easy_conn.quit()
以上這篇通過python 執(zhí)行 nohup 不生效的解決就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python網絡編程學習筆記(二):socket建立網絡客戶端
看了這一節(jié),突然之間對python網絡編程學習筆記(1)中的一些不理解的問題有了認識,至少明白了socket是怎么回事。這里關于socket的起源等問題就不做筆記記錄了,直接進入主題2014-06-06升級keras解決load_weights()中的未定義skip_mismatch關鍵字問題
這篇文章主要介紹了升級keras解決load_weights()中的未定義skip_mismatch關鍵字問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06