Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从cron更改IP脚本不起作用?python_Python_Cron_Subprocess_Crontab - Fatal编程技术网

从cron更改IP脚本不起作用?python

从cron更改IP脚本不起作用?python,python,cron,subprocess,crontab,Python,Cron,Subprocess,Crontab,我制作了此脚本以更改eth0接口的ip。它在控制台上运行正常,但在cron上不工作。即使脚本在cron上没有错误。请指导我完成此操作 #!/usr/bin/python import subprocess import os ip=['192.168.X.X','192.168.X.X','192.168.X.X'] gateway='192.168.X.1' netmask='225.225.225.0' currentip=os.popen("ifconfig eth0 | grep '

我制作了此脚本以更改eth0接口的ip。它在控制台上运行正常,但在cron上不工作。即使脚本在cron上没有错误。请指导我完成此操作

#!/usr/bin/python
import subprocess
import os
ip=['192.168.X.X','192.168.X.X','192.168.X.X']
gateway='192.168.X.1'
netmask='225.225.225.0'


currentip=os.popen("ifconfig eth0 | grep 'inet addr' | cut -d: -f2 | awk '{ print $1}'").read()
currentip=currentip.strip()
print currentip
lip=len(ip)

for item in ip:
    index=ip.index(item)
    if item==currentip:

        if index==lip-1:        
            index=0
            newip=ip[index]
            subprocess.call(['ifconfig','eth0',newip,'netmask',netmask,'up'])
            subprocess.call(['route','add','default','gw',gateway])
        else:
            index=index+1
            newip=ip[index]
            print newip
            subprocess.call(['ifconfig','eth0',newip,'netmask',netmask,'up'])
            subprocess.call(['route','add','default','gw',gateway])
Cron代码是

06 12 * * * cd /root/system/; python ip.py

对我来说,这似乎是一个“绝对路径”问题。 请为文件和Python编译器使用绝对路径


06 12***/usr/bin/python/root/system/ip.py

我将抛开对从python调用grep、cut和awk的厌恶。作业不在cron中工作的最常见原因是环境不同。如果启动文件中存在程序所依赖的任何内容,如
.profile
.bash\u profile
,则该文件将不存在<例如,代码>$PATH,以及当前目录。最好给cron一个python程序的完整路径名,并在ifconfig、grep。。。