欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

通過(guò)python 執(zhí)行 nohup 不生效的解決

 更新時(shí)間:2020年04月16日 11:03:41   作者:時(shí)光重返七十年  
這篇文章主要介紹了通過(guò)python 執(zhí)行 nohup 不生效的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

通過(guò)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進(jìn)程并沒(méi)有繼續(xù)運(yùn)行,這可能是因?yàn)閑xec_command執(zhí)行完之后立刻關(guān)閉通道的原因,換用invoke_shell可以正常運(yùn)行:

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和延時(shí)必不可少

補(bǔ)充知識(shí):paramiko遠(yuǎn)程服務(wù)器nohup阻塞問(wèn)題

一、需求描述:

需要來(lái)回切換多臺(tái)服務(wù)器(腳本命令不太熟),就用了python的paramiko模塊進(jìn)行遠(yuǎn)程連接服務(wù)器,控制程序的停止和啟動(dòng)。安裝:pip install paramiko

二、問(wèn)題描述:

import paramiko
 
# 創(chuàng)建SSH對(duì)象
ssh = paramiko.SSHClient()
# 允許連接不在know_hosts文件中的主機(jī)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 連接服務(wù)器
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 &')
# 獲取命令結(jié)果
result = stdout.read()
# 關(guān)閉連接
ssh.close()

這樣連接服務(wù)器的時(shí)候確實(shí)可以執(zhí)行,但是遇到會(huì)阻塞的任務(wù)時(shí),就無(wú)法生效,找了很多方法,最后發(fā)現(xiàn)這個(gè)比較有效。

三、解決方法

import paramiko
 
# 創(chuàng)建SSH對(duì)象
ssh = paramiko.SSHClient()
# 允許連接不在know_hosts文件中的主機(jī)
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 連接服務(wù)器
ssh.connect(hostname='192.168.0.3', port=22, username='xxx', key=private_key)
# 添加下面代碼
transport = ssh.get_transport()
channel = transport.open_session()
# 執(zhí)行命令 此方法沒(méi)有返回值
channel.exec_command('cd ~/ ; nohup python3.6 run_test.py > nohup_test.log 2>&1 &')
 
# 關(guān)閉連接
ssh.close()

四、類的調(diào)用實(shí)現(xiàn):

簡(jiǎn)單測(cè)試,見(jiàn)下面代碼

# -*- coding: utf-8 -*-
"""
20190330
"""
 
import paramiko
import time
from confs.log import logger # 自行導(dǎo)入logging模塊即可
 
 
class EasyConnectHandle(object):
  """操作遠(yuǎn)程服務(wù)器"""
 
  def __init__(self, connect_host_name:dict):
    """初始化參數(shù)"""
    """
      "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()) # 允許連接陌生服務(wù)器
    self.ssh.connect(hostname=self.connect_host["ip"], port=22, username=self.connect_host["user_name"],
             password=self.connect_host["pwd"], timeout=10) # 初始化的時(shí)候連接到新的服務(wù)器
    logger.info(f"登錄服務(wù)器---{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):
    """斷開(kāi)服務(wù)器"""
    self.ssh.close()
    logger.info(f"退出服務(wù)器---{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"服務(wù)器---{easy_conn.connect_host['ip']}執(zhí)行---cd {job['path']};{job['type']}---成功")
        time.sleep(2)
    else:
      logger.info(f"服務(wù)器---{easy_conn.connect_host['ip']}暫時(shí)沒(méi)有任務(wù)")
    easy_conn.quit()

以上這篇通過(guò)python 執(zhí)行 nohup 不生效的解決就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • pytorch 把圖片數(shù)據(jù)轉(zhuǎn)化成tensor的操作

    pytorch 把圖片數(shù)據(jù)轉(zhuǎn)化成tensor的操作

    這篇文章主要介紹了pytorch 把圖片數(shù)據(jù)轉(zhuǎn)化成tensor的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • python基礎(chǔ)中的文件對(duì)象詳解

    python基礎(chǔ)中的文件對(duì)象詳解

    這篇文章主要為大家介紹了python基礎(chǔ)中的文件對(duì)象,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01
  • Python使用psutil獲取系統(tǒng)信息

    Python使用psutil獲取系統(tǒng)信息

    這篇文章介紹了Python使用psutil獲取系統(tǒng)信息的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(二):socket建立網(wǎng)絡(luò)客戶端

    python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(二):socket建立網(wǎng)絡(luò)客戶端

    看了這一節(jié),突然之間對(duì)python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(1)中的一些不理解的問(wèn)題有了認(rèn)識(shí),至少明白了socket是怎么回事。這里關(guān)于socket的起源等問(wèn)題就不做筆記記錄了,直接進(jìn)入主題
    2014-06-06
  • python中entry用法講解

    python中entry用法講解

    在本篇文章里小編給大家整理的是一篇關(guān)于python中entry用法講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)參考下。
    2020-12-12
  • Python StringIO如何在內(nèi)存中讀寫(xiě)str

    Python StringIO如何在內(nèi)存中讀寫(xiě)str

    這篇文章主要介紹了python StringIO如何在內(nèi)存中讀寫(xiě)str,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • Python+Tkinter簡(jiǎn)單實(shí)現(xiàn)注冊(cè)登錄功能

    Python+Tkinter簡(jiǎn)單實(shí)現(xiàn)注冊(cè)登錄功能

    這篇文章主要為大家詳細(xì)介紹了Python+Tkinter簡(jiǎn)單實(shí)現(xiàn)注冊(cè)登錄功能,連接本地MySQL數(shù)據(jù)庫(kù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多線程TCP服務(wù)器的教程

    用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多線程TCP服務(wù)器的教程

    這篇文章主要介紹了用Python實(shí)現(xiàn)一個(gè)簡(jiǎn)單的多線程TCP服務(wù)器的教程,示例的運(yùn)行環(huán)境為Windows操作系統(tǒng),需要的朋友可以參考下
    2015-05-05
  • 升級(jí)keras解決load_weights()中的未定義skip_mismatch關(guān)鍵字問(wèn)題

    升級(jí)keras解決load_weights()中的未定義skip_mismatch關(guān)鍵字問(wèn)題

    這篇文章主要介紹了升級(jí)keras解決load_weights()中的未定義skip_mismatch關(guān)鍵字問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • tensorflow的計(jì)算圖總結(jié)

    tensorflow的計(jì)算圖總結(jié)

    這篇文章主要介紹了tensorflow的計(jì)算圖總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01

最新評(píng)論