huanpipeng0560 2023-02-23 15:17 采纳率: 66.7%
浏览 35
已结题

paramiko执行nohup无效果

paramiko执行nohup无效,后台进程是起来的,但是没数据生成

img

但是,我手动执行nohup命令就没问题

img


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2023/2/7 17:29
# @Author : LinQ
import sys
import time

import paramiko

from Common.logger.logger import Log

log = Log().logger


class sshClient(object):
    def __init__(self, port, hostname, username, password):
        # 1 创建ssh_client实例
        self.ssh_client = paramiko.SSHClient()
        # 自动处理第一次连接的yes或者no的问题
        self.ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy)
        self.port = port
        self.hostname = hostname
        self.username = username
        self.password = password
        self.ssh_client.connect(
            port=self.port,
            hostname=self.hostname,
            username=self.username,
            password=self.password
        )

    def exec_command(self, shell_command):
        stdin, stdout, stderr = self.ssh_client.exec_command(shell_command)
        log.info("shell_command:%s", shell_command)
        stdout_info = stdout.read().decode('utf-8')
        log.info("stdout_info:%s", stdout_info)
        stderr_info = stderr.read().decode('utf-8')
        log.info("stderr_info:%s", stderr_info)

    def exec_commands(self, shell_commands):
        for shell_command in shell_commands:
            self.exec_command(shell_command)

    def invoke_shell(self, shell_command):
        channel = self.ssh_client.invoke_shell()
        channel.send(shell_command)
        time.sleep(3)
        # stdin, stdout, stderr = self.ssh_client.exec_command(shell_command)
        # log.info("shell_command:%s", shell_command)
        # stdout_info = stdout.read().decode('utf-8')
        # log.info("stdout_info:%s", stdout_info)
        # stderr_info = stderr.read().decode('utf-8')
        # log.info("stderr_info:%s", stderr_info)


if __name__ == '__main__':
    sshclient = sshClient("22", "10.21.16.45", "root", "asiainfo123")
    sshclient.exec_command(". ./.bash_profile ; nohup sh /home/datacollection_sdp/getCPU.sh  >xx.log & ")
    sshclient.exec_command("cd /home/datacollection_sdp;ll ")

shell脚本内容

#!/bin/bash
while true
do
    sar -r 1 1  | tail -1 | awk '{print systime(),$2,$3,$4,$5,$6,$7,$8,$9,$10,$11 }'>>cpu.txt
        sleep 1

done
  • 写回答

4条回答 默认 最新

  • CCPro- 2023-02-23 15:27
    关注

    根据您提供的代码和信息,可能是以下原因导致问题:

    1.nohup命令执行后,标准输出和标准错误输出不会立即刷新到文件,而是在进程退出后才会刷新,这可能会导致进程已经启动,但是日志文件还没有被创建或写入。
    解决方法:您可以在nohup命令执行后,加一个sleep命令等待一段时间,让进程有足够的时间启动和写入日志文件。例如,将sshclient.exec_command(". ./.bash_profile ; nohup sh /home/datacollection_sdp/xx.sh >xx.log & ")改为sshclient.exec_command(". ./.bash_profile ; nohup sh /home/datacollection_sdp/xx.sh >xx.log & sleep 5")。

    2.您的脚本命令中使用了相对路径./.bash_profile,可能导致无法正确加载.bash_profile文件中的环境变量。如果.bash_profile文件不在当前目录下,那么需要指定完整路径。
    解决方法:您可以将.bash_profile文件的路径改为绝对路径,例如/root/.bash_profile。

    3.您的脚本命令中使用了相对路径./xx.sh,可能导致无法正确找到xx.sh文件。如果xx.sh文件不在当前目录下,那么需要指定完整路径。
    解决方法:您可以将xx.sh文件的路径改为绝对路径,例如/home/datacollection_sdp/xx.sh。

    4.您的脚本命令没有指定工作目录,可能导致无法正确找到cpu.txt文件的路径。如果cpu.txt文件不在当前目录下,那么需要指定完整路径。
    解决方法:您可以在xx.sh脚本中加上cd命令,将工作目录切换到cpu.txt所在的目录,例如:

    #!/bin/bash
    cd /home/datacollection_sdp/
    while true
    do
        sar -r 1 1  | tail -1 | awk '{print systime(),$2,$3,$4,$5,$6,$7,$8,$9,$10,$11 }'>>cpu.txt
        sleep 1
    done
    

    希望这些解决方法能够帮助您解决问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 3月3日
  • 已采纳回答 2月23日
  • 修改了问题 2月23日
  • 修改了问题 2月23日
  • 展开全部

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!