从 crontab 运行时 subprocess.popen 似乎失败
我正在从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
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.
您可能需要向 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.
对于 python 中与 ssh 相关的所有内容,您可以考虑使用 paramiko。使用它,以下代码应该可以完成您想要的操作。
For everything ssh-related in python, you might consider using paramiko. Using it, the following code should do what you want.
正如 user1652558 指出的那样,当从 cron 运行 python 脚本时,环境
PATH
可能会挂起。要使用示例代码扩展此答案,以将自定义
PATH
值添加到subprocess
调用的环境中: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 asubprocess
call: