Python 调用 `arp` 的脚本在 crontab 运行 时产生错误
Python script calling `arp` produces error when run from crontab
我的服务器:
Linux dhcpns 3.19.0-28-generic #30-Ubuntu SMP Mon Aug 31 15:52:51 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
我的 Python 脚本:
#! /usr/bin/python
import syslog, traceback
import subprocess as sp
def getarp():
cmd = ["arp", "-a"]
arp = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
output, err = arp.communicate()
entries = output.splitlines()
return entries
def syslog_trace(trace):
log_lines = trace.split('\n')
for line in log_lines:
if len(line):
syslog.syslog(syslog.LOG_ALERT,line)
if __name__ == '__main__':
try:
lstArp = getarp()
print lstArp
except Exception as e:
syslog.syslog(syslog.LOG_ALERT,e.__doc__)
syslog_trace(traceback.format_exc())
raise
当我 运行 来自命令行的脚本时,它会产生预期的输出。
但是,当我从 crontab -e
调用脚本时(显然是将输出重定向到文件)脚本失败。
日志:
Sep 28 18:36:01 dhcpns testarp.py[9984]: OS system call failed.
Sep 28 18:36:01 dhcpns testarp.py[9984]: Traceback (most recent call last):
Sep 28 18:36:01 dhcpns testarp.py[9984]: File "/home/ubuntu/testarp.py", line 23, in <module>
Sep 28 18:36:01 dhcpns testarp.py[9984]: lstArp = getarp()
Sep 28 18:36:01 dhcpns testarp.py[9984]: File "/home/ubuntu/testarp.py", line 10, in getarp
Sep 28 18:36:01 dhcpns testarp.py[9984]: arp = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
Sep 28 18:36:01 dhcpns testarp.py[9984]: File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
Sep 28 18:36:01 dhcpns testarp.py[9984]: errread, errwrite)
Sep 28 18:36:01 dhcpns testarp.py[9984]: File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
Sep 28 18:36:01 dhcpns testarp.py[9984]: raise child_exception
Sep 28 18:36:01 dhcpns testarp.py[9984]: OSError: [Errno 2] No such file or directory
卧槽?
我错过了什么?
通过 cron 运行 的命令会继承不同的环境。特别是,PATH
envvar 可能不同。要解决此问题,请在 Python 脚本中指定 arp
程序的完整路径或在相应的 crontab 中显式配置 PATH
:
PATH=/bin:/usr/bin:/path/where/arp/lives
我的服务器:
Linux dhcpns 3.19.0-28-generic #30-Ubuntu SMP Mon Aug 31 15:52:51 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
我的 Python 脚本:
#! /usr/bin/python
import syslog, traceback
import subprocess as sp
def getarp():
cmd = ["arp", "-a"]
arp = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
output, err = arp.communicate()
entries = output.splitlines()
return entries
def syslog_trace(trace):
log_lines = trace.split('\n')
for line in log_lines:
if len(line):
syslog.syslog(syslog.LOG_ALERT,line)
if __name__ == '__main__':
try:
lstArp = getarp()
print lstArp
except Exception as e:
syslog.syslog(syslog.LOG_ALERT,e.__doc__)
syslog_trace(traceback.format_exc())
raise
当我 运行 来自命令行的脚本时,它会产生预期的输出。
但是,当我从 crontab -e
调用脚本时(显然是将输出重定向到文件)脚本失败。
日志:
Sep 28 18:36:01 dhcpns testarp.py[9984]: OS system call failed.
Sep 28 18:36:01 dhcpns testarp.py[9984]: Traceback (most recent call last):
Sep 28 18:36:01 dhcpns testarp.py[9984]: File "/home/ubuntu/testarp.py", line 23, in <module>
Sep 28 18:36:01 dhcpns testarp.py[9984]: lstArp = getarp()
Sep 28 18:36:01 dhcpns testarp.py[9984]: File "/home/ubuntu/testarp.py", line 10, in getarp
Sep 28 18:36:01 dhcpns testarp.py[9984]: arp = sp.Popen(cmd, stdout=sp.PIPE, stderr=sp.PIPE)
Sep 28 18:36:01 dhcpns testarp.py[9984]: File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
Sep 28 18:36:01 dhcpns testarp.py[9984]: errread, errwrite)
Sep 28 18:36:01 dhcpns testarp.py[9984]: File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
Sep 28 18:36:01 dhcpns testarp.py[9984]: raise child_exception
Sep 28 18:36:01 dhcpns testarp.py[9984]: OSError: [Errno 2] No such file or directory
卧槽? 我错过了什么?
通过 cron 运行 的命令会继承不同的环境。特别是,PATH
envvar 可能不同。要解决此问题,请在 Python 脚本中指定 arp
程序的完整路径或在相应的 crontab 中显式配置 PATH
:
PATH=/bin:/usr/bin:/path/where/arp/lives