python sh package can't execute in crontab[sh.CommandNotFound]
来源:互联网 发布:protobuf3 java 编辑:程序博客网 时间:2024/05/19 03:46
crontab中,sh.ifconfig()
执行不不了,报错类似如下
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
网上找,很多说是PATH
的事情,第一反应是按照sys.path
去找,比如这个crontab中运行python程序出错,提示ImportError: No module named解决全过程, 结果比较了一下,发现一样!所以不是python版本的事
, import sh
也可以一直执行。浪费了一晚上到三点找不到方法,没戏就睡了!
今天决定跟着源码去定位,终于找到这家伙了。
/usr/local/lib/python2.7/dist-packages/sh.py def which(program): def is_exe(fpath): return (os.path.exists(fpath) and os.access(fpath, os.X_OK) and os.path.isfile(os.path.realpath(fpath))) fpath, fname = os.path.split(program) #lucifer #print 'fpath={0},fname={1}'.format(fpath,fname) if fpath: if is_exe(program): return program else: if "PATH" not in os.environ: return None for path in os.environ["PATH"].split(os.pathsep): exe_file = os.path.join(path, program) if is_exe(exe_file): return exe_file return None
上面说的是,比如你执行sh.ifconfig()
,就需要知道ifconfig
的位置,从os.environ[`PATH`]
中去找,找不到就甩出来这个异常。
所以只要在自己的脚本中加上下面这句话就OK了
#Crontab PATH is not same compared with user.path#This is for sh.commandsif '/sbin' not in os.environ['PATH']: os.environ['PATH'] += ':/sbin'
先说说怎么测试是不是这个问题呢:
bash : which ifconfig/sbin/ifconfig#test.pyimport shimport osprint os.environ['PATH']print sh.ifconfig()
两个直接执行,一个用crontab
后台执行并输出,比较下就可以看到了。
源码调试中,可以发现在crontab
下找不到ifconfig
位置,但是不用crontab
就可以。就可以确定是这个问题引起的。
果然,源码大法好!!!
之所以提这个,是因为很多人说是python lib path
的事情,测试后发现不是,sh
包可以找到,但sh
本身执行也需要PATH
,一些第三方package
偶尔会出这个问题。
0 0
- python sh package can't execute in crontab[sh.CommandNotFound]
- sh: can't access tty; job control turned off 解决方法
- /bin/sh: can't access tty; job control turned off
- /bin/sh: can't access tty; job control turned off
- /bin/sh:can't access tty;job control turned off
- errors in crontab file,can't install
- /bin/sh: can
- sh
- crontab 执行脚本***.sh 方法
- crontab 执行脚本***.sh 方法
- python 调用sh文件
- 爬坑简(pick)史(sh*t)
- busybox "sh: can't access tty; job control turned off" 问题的解决
- 问题:/bin/sh: can't access tty; job controlturned off 解决记录
- 出现/bin/sh: can't access tty; job control turned off的解决方案
- android系统移植报错:sh: can't access tty; job control turned off
- 出现/bin/sh: can't access tty; job control turned off的解决方案
- Solution : Can't quit Packagekit - adding software package in OpenSuse
- Spring MVC 笔记
- CS231n Neural Networks Part 2: Setting up the Data and the Loss
- Java中split中需转意的符号
- View的animate驱动动画
- How to install sublime text 3 in ubantu linux
- python sh package can't execute in crontab[sh.CommandNotFound]
- HTML基础3
- Python的random
- 用JS替 <input type=file>的value属性动态赋值
- Android布局动画
- Qt-第一个白窗口程序
- 栈的顺序存储实现
- 8月21号晚11点40,人生第一次开博客
- html介绍。