2015-05-29 82 views
1

我正在研究raspbian並編寫了一個python腳本,它通過RS232與一些硬件相關聯,用於覆蓋樹莓的IO狀態。它也寫入日誌文件。Cronjob:Python腳本沒有寫入文件

一切工作正常,當我開始從命令行腳本: [email protected] ~/scripts $ python steppercontrol.py

我添加了憑證作爲一個cronjob(sudo crontab -e

SHELL=/bin/bash 
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games 
@reboot /usr/bin/python /home/pi/scripts/steppercontrol.py 

該腳本並重新啓動後運行,但日誌文件不寫 系統日誌給出以下內容

cat /var/log/syslog | grep CRON 
May 29 12:05:16 raspberrypi /USR/SBIN/CRON[2106]: (CRON) info (No MTA installed, discarding output) 
May 29 12:17:01 raspberrypi /USR/SBIN/CRON[2456]: (root) CMD ( cd/&& run-parts --report /etc/cron.hourly) 
May 29 13:17:01 raspberrypi /USR/SBIN/CRON[2509]: (root) CMD ( cd/&& run-parts --report /etc/cron.hourly) 

chmods應該可以:

[email protected] ~/scripts $ ls -lh 
total 16K 
-rwxr-xr-x 1 pi pi 3.1K May 27 12:55 steppercontrol.py 
-rwxrwxrwx 1 pi pi 249 May 29 12:05 stepperlog 

IMO它和python本身沒有關係。我也無法設法將腳本(作爲cronjob)的stdout重定向到一個文件。我迷路了,這裏是我的腳本:

順便說一句:這是我的第一個python腳本,通常我是不是很好的Linux,但raspbian和谷歌讓一切變得簡單;-)

import serial 
import time 
import pifacedigitalio as p 
import datetime 

# function to read data by busy waiting 
# timeout is enable, non blocking 
def getData(p): 
    "get data busy waiting" 

    d = '' 
    if p.inWaiting() <= 0: 
    return d 

    time.sleep(0.3) 
    while p.inWaiting() > 0: 
     d += p.read(1) 
    return d 
# end of function 

# main program logig 
# init serial communication 
port = serial.Serial("/dev/ttyUSB0", bytesize=serial.EIGHTBITS, baudrate=9600, stopbits=serial.STOPBITS_TWO, timeout=1.0) 
p.init() 
for i in range(0,8): 
    p.digital_write(i,0) 
    p.digital_write_pullup(i, 1) 

logfile = open('/home/pi/scripts/stepperlog','a') 
i = datetime.datetime.now() 
logfile.write(str(i) + " script started \n") 
print(str(i) + " script started \n") 

# query hello world string and write answer to screen 
port.write("?ver\r") 
d = getData(port) 
print(">> " + d + "\n") 
port.write("!a\r") 

# setup stepper drive 
port.write("!axis 1 0 0\r")   # disable all axis excep X-axis 
port.write("!pitch 1 1 1\r")   # set pitch of rod ... needed? 
port.write("!cur 1 1 1\r")    # set current per motor to 1 A 
port.write("!accel 0.5 0.5 0.5\r")  # set accelleration to value in m/s^2 
port.write("!velfac 0.1 0.1 0.1\r") # reduce speed by facor 1/10 

pinList = [0,0,0,0,0,0] 
prevSelection = -2 

while 1: 
    for i in range(0,6): 
     pinList[i] = p.digital_read(i) 
     p.digital_write(i,pinList[i]) 
     #print(">> I/O " + str(i) + " : " + str(pinList[i])) 

    speed = 0; 
    curSelection = -1 

    if pinList[0] == 1:  # position 1 
     speed = 5;   # move down fast 5mm/s 
     curSelection = 0 
    elif pinList[1] == 1: # position 3 
     speed = -0.1;   # move up 50 um/s 
     curSelection = 1 
    elif pinList[2] == 1: # position 5 
     speed = -0.2;   # move up 100 um/s 
     curSelection = 2 
    elif pinList[3] == 1: # position 7 
     speed = -0.3;   # move up 100 um/s 
     curSelection = 3 
    elif pinList[4] == 1: # position 9 
     speed = -0.4;   # move up 100 um/s 
     curSelection = 4 
    elif pinList[5] == 1: # position 11 
     speed = -5;   # move up fast 5 mm/s 
     curSelection = 5 

    calcspeed = float(speed) 
    calcspeed *= 10/1.36    # factor 10/100 corresponds to speed reduction from above ! 

    if curSelection != prevSelection: 
     i = datetime.datetime.now() 
     logfile.write(str(i) + " " + str(prevSelection) + " " + str(curSelection) + " " + str(speed) + " " + str(calcspeed) + "\n") 
     print(str(i) + " " + str(prevSelection) + " " + str(curSelection) + " " + str(speed) + " " + str(calcspeed)) 
    prevSelection = curSelection 

    speed = "%.10f" % calcspeed   # float to string 
    port.write("!speed" + speed + "\r") 

    wait = 0.1 
    time.sleep(float(wait)) 
+0

的cron不跑你的個人資料的.bashrc,所以適當的環境變量並不總是設置。如果我不得不猜測,我會認爲你錯過了你的'PYTHONPATH'。您可以從終端回顯$ PYTHONPATH,看看它應該如何正確運行腳本,然後在您的crontab中添加一行'PYTHONPATH = ...'來設置它。 –

+0

檢查,... PYTHONPATH確實讓我感到困惑,因爲它在我的終端(bash)中是emtpty,而腳本運行得很好'pi @ raspberrypi〜$ echo $ PYTHONPATH'....編輯這裏沒有新行可見...輸出回聲是沒有什麼 – user3002004

回答

0

最終答案很明顯! 我沒有刷新輸出。對於標準輸出,標準錯誤:

import sys 
sys.stdout.flush() 
sys.stderr.flush() 

爲打開文件:

logfile.flush() 
0

你的cron作業正在向stdout或stderr寫入內容--cron收集並將其郵寄給你,但是你的系統沒有安裝MTA(因此日誌消息「No MTA installed」)。嘗試將stderr和stdout重定向到一個文件,你可能會得到更多關於問題的信息。

或者,你可以安裝一個MTA,讓它給你的輸出 - 這將幫助你和你的未來的後臺任務遇到任何其他問題:

命令和apt-get安裝後綴

+0

跆拳道是錯誤的這個編輯窗口...我如何添加換行符?我很抱歉格式化,這是我的文本:.......................我試着重定向輸出(stderr和stdout)......''@ reboot/usr/bin/python /home/pi/scripts/steppercontrol.py >> /home/pi/scripts/log.txt 2>&1 #*/1 * * * */usr/bin/python/home/pi/scripts/testlog.py >> /home/pi/scripts/log.txt 2>&1'' ..............第一行不起作用,第二行 – user3002004

+0

isntalling postfix不會改變任何東西。沒有想到它會做,但嘗試過。 ......有些奇怪。上面的第二個腳本每分鐘執行一次,完成同樣的事情(寫入文件和stdout,兩者都被記錄並接收)。時間表(@reboot和每分鐘) – user3002004