2013-04-21 70 views
1

所以我正在监视众多文件的修改时间。当文件更新时,我通过ssh将它复制到另一台机器上。这里是什么,我有一个SSCCE:python os.system似乎无端挂起

import os 
import time 

send = "/home/pi/PythonScripts/tempData.txt" 
check = "/home/pi/PythonScripts/check.txt" 

statbuf = os.stat(send) 
print "Modification time:",statbuf.st_mtime 

def wr2(data): 
    file2 = open(check, 'w') 
    file2.write(str(data)) 
    file2.close() 
    return 0 

def rd(): 
    file = open(send, 'r') 
    line2 = file.readline() 
    file.close() 
    return line2 

def rd2(): 
    file2 = open(check, 'r') 
    line2 = file2.readline() 
    file2.close() 
    return line2 


while(run): 
    try: 
    statbuf = os.stat(send) 
    line2 = rd2() 
    print line2 

    if (str(statbuf.st_mtime) == line2): 
     print "File has not changed...\n" 
     time.sleep(1) 
    else: 
     data = rd() 
     print "Data in File: " + data 
     os.system("sudo scp /home/pix/PythonScripts/tempData.txt server1:/home/tix/Server1_SSH/Real_Data.txt") 
     wr2(statbuf.st_mtime) 
     print "New Modification Time:",statbuf.st_mtime 
     time.sleep(1) 



    except (KeyboardInterrupt, SystemExit): 
     print '\nKeyboard Interrupt Caught!' 
     run = 0 
     raise 

所以当它到达os.system()命令它挂在那儿没有做任何事情......但是当我在Python解释器运行完全相同的代码,它工作得很好。我似乎无法理解这个问题会是什么......任何帮助都会很棒!

回答

2

sudo是可能的罪魁祸首,并可能要求输入密码。

而不是os.system,请尝试使用subprocess module来代替。这将让你看到标准输出stderr流,看看发生了什么。

此外,我会质疑在脚本中使用sudo的做法。通常,决定使用sudo将留给调用Python脚本的人。

+0

我也这么认为,但是在python解释器中,它工作得很好,没有任何问题... – skbeez 2013-04-21 00:49:23

+1

其实你是对的,我删除了sudo,现在它完美地工作。谢谢! – skbeez 2013-04-21 01:59:23