代码在控制台中工作,但在cron运行时则不行

时间:2016-01-07 12:11:10

标签: python cron

当我运行此代码时,适用于控制台。它不会产生错误。

import sh
import uuid
import urllib

print 'Ip ....'
# Print only IP address from specific interface
LineaX = sh.grep(sh.ifconfig('eth0'), '-oP', '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
# Asignar Variables
IpX = LineaX.splitlines()[0]
PuertaX= LineaX.splitlines()[1]
MascaraX = LineaX.splitlines()[2]
MacX = ':'.join(['{:02x}'.format((uuid.getnode() >> i) & 0xff) for i in range(0,8*6,8)][::-1])
DataX = open('/etc/resolv.conf').read().split()
for item in DataX:
    if len( item.split(".") ) == 4:
        Dns1X = item
        Dns2X = '000.000.000.000'
    # Actualizar informacion
urllib.urlopen('http://www.miurl.com/file.php?mac='+ MacX + '&ip=' + IpX + '&mascara='+ MascaraX + '&puerta='+ PuertaX + '&dns1='+ Dns1X + '&dns2='+ Dns2X )
print 'Ip ... OK'

在cronjob中产生此错误:

Traceback (most recent call last):
  File "/home/pi/system/sensor_ip.py", line 7, in <module>
    LineaX = sh.grep(sh.ifconfig('eth0'), '-oP', '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 2301, in __getattr__
    return self.__env[name]
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 2232, in __getitem__
    return Command._create(k, **self.baked_args)
  File "/usr/local/lib/python2.7/dist-packages/sh.py", line 776, in _create
    raise CommandNotFound(program)
sh.CommandNotFound: ifconfig

我有什么不对的?

2 个答案:

答案 0 :(得分:0)

使用Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> f = "C:/test/UpdatePackage/filelist" >>> import os >>> os.path.isfile(f) True >>> ,您需要导入所需的命令,例如:

sh

这就是您看到错误的原因:

from sh import ifconfig

如果它在shell上正常工作但在cron中没有工作,则问题是你需要指定二进制文件的绝对路径,因为crons没有sh.CommandNotFound: ifconfig

答案 1 :(得分:0)

我在直接阅读sh.py时解决了同样的问题!

由于crontab有运行shell或脚本的OWN PATH,sh.py执行306 fpath, fname = os.path.split(program),因此返回空,这意味着无法找到ifconfig位置PATH,所以抛出NONE错误。

 # /usr/local/lib/python2.7/dist-packages/sh.py
 300 def which(program):
 301     def is_exe(fpath):
 302         return (os.path.exists(fpath) and
 303                 os.access(fpath, os.X_OK) and
 304                 os.path.isfile(os.path.realpath(fpath)))
 305
 306     fpath, fname = os.path.split(program)
 307     #lucifer
 308     #print 'fpath={0},fname={1}'.format(fpath,fname)
 309     if fpath:
 310         if is_exe(program):
 311             return program
 312     else:
 313         if "PATH" not in os.environ:
 314             return None
 315         for path in os.environ["PATH"].split(os.pathsep):
 316             exe_file = os.path.join(path, program)
 317             if is_exe(exe_file):
 318                 return exe_file

因此您需要在os.environ中添加命令(例如ifconfig路径 我将它添加到我的script.py for crontab

#Crontab PATH is not same compared with user.path
#This is for sh.commands
if '/sbin' not in os.environ['PATH']:
    os.environ['PATH'] += ':/sbin'

宾果!一切都好!

相关问题