从 crontab 运行时 subprocess.popen 似乎失败

发布于 2024-09-24 08:12:59 字数 1105 浏览 3 评论 0原文

我正在从 crontab 运行一个脚本,该脚本将仅通过 ssh 运行命令并将结果存储在文件中。

似乎失败的函数是subprocess.popen

这是 python 函数:

def _executeSSHCommand(sshcommand,user,node):

    '''
    Simple function to execute an ssh command on a remote node.
    '''

    sshunixcmd = '/usr/bin/ssh %s@%s \'%s\'' % (user,node,sshcommand)
    process = subprocess.Popen([sshunixcmd],
                                shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
    process.wait()
    result = process.stdout.readlines()
    return result

当它从命令行运行时,它执行正确,从 cron 运行时它似乎失败并显示以下错误消息。

以下是 crontab 条目:

02 * * * * /home/matt/scripts/check-diskspace.py >>> /home/matt/logs/disklog.log

以下是错误:

Sep 23 17:02:01 timmy CRON[13387]: (matt) CMD (/home/matt/scripts/check-diskspace.py >> /home/matt/logs/disklog.log)
Sep 23 17:02:01 timmy CRON[13386]: (CRON) error (grandchild #13387 failed with exit status 2)

我正在盲目地试图准确地找到我出错的地方。有什么想法吗?

I'm running a script from crontab that will just ssh and run a command and store the results in a file.

The function that seems to be failing is subprocess.popen.

Here is the python function:

def _executeSSHCommand(sshcommand,user,node):

    '''
    Simple function to execute an ssh command on a remote node.
    '''

    sshunixcmd = '/usr/bin/ssh %s@%s \'%s\'' % (user,node,sshcommand)
    process = subprocess.Popen([sshunixcmd],
                                shell=True,
                                stdout=subprocess.PIPE,
                                stderr=subprocess.PIPE)
    process.wait()
    result = process.stdout.readlines()
    return result

When it's run from the command line, it executes correctly, from cron it seems to fail with the error message below.

Here are the crontab entries:

02 * * * * /home/matt/scripts/check-diskspace.py >> /home/matt/logs/disklog.log

Here are the errors:

Sep 23 17:02:01 timmy CRON[13387]: (matt) CMD (/home/matt/scripts/check-diskspace.py >> /home/matt/logs/disklog.log)
Sep 23 17:02:01 timmy CRON[13386]: (CRON) error (grandchild #13387 failed with exit status 2)

I'm going blind trying to find exactly where I have gone so wrong. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

何以畏孤独 2024-10-01 08:12:59

cron 路径非常有限。您应该将绝对路径设置为 ssh /usr/bin/ssh 或将 PATH 设置为 crontab 中的第一行。

The cron PATH is very limited. You should either set absolute path to your ssh /usr/bin/ssh or set the PATH as a first line in your crontab.

画尸师 2024-10-01 08:12:59

您可能需要向 ssh 传递 -i 参数来告诉 ssh 使用特定的密钥文件。问题是您的环境未设置为告诉 ssh 使用哪个密钥。

事实上,你在这里使用 python 有点转移注意力。

You probably need to pass ssh the -i argument to tell ssh to use a specific key file. The problem is that your environment is not set up to tell ssh which key to use.

The fact that you're using python here is a bit of a red herring.

梦回梦里 2024-10-01 08:12:59

对于 python 中与 ssh 相关的所有内容,您可以考虑使用 paramiko。使用它,以下代码应该可以完成您想要的操作。

import paramiko
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(node, username=user)
stdout = client.exec_command(ssh_command)[0]
return stdout.readlines()

For everything ssh-related in python, you might consider using paramiko. Using it, the following code should do what you want.

import paramiko
client = paramiko.SSHClient()
client.load_system_host_keys()
client.connect(node, username=user)
stdout = client.exec_command(ssh_command)[0]
return stdout.readlines()
帝王念 2024-10-01 08:12:59

正如 user1652558 指出的那样,当从 cron 运行 python 脚本时,环境 PATH 可能会挂起。

要使用示例代码扩展此答案,以将自定义 PATH 值添加到 subprocess 调用的环境中:

import os
import subprocess

#whatever user PATH values you need
my_path = "/some/custom/path1:/some/custom/path2"

#append the custom values to the current PATH settings
my_env = os.environ.copy()
my_env["PATH"] = my_path + ":" + my_env["PATH"]

#subprocess call
resp = subprocess.check_output([cmd], env=my_env, shell=True)

When running python scripts from cron, the environment PATH can be a hangup, as user1652558 points out.

To expand on this answer with example code to add custom PATH values to the environment for a subprocess call:

import os
import subprocess

#whatever user PATH values you need
my_path = "/some/custom/path1:/some/custom/path2"

#append the custom values to the current PATH settings
my_env = os.environ.copy()
my_env["PATH"] = my_path + ":" + my_env["PATH"]

#subprocess call
resp = subprocess.check_output([cmd], env=my_env, shell=True)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文