我正在研究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))
的cron不跑你的個人資料的.bashrc,所以適當的環境變量並不總是設置。如果我不得不猜測,我會認爲你錯過了你的'PYTHONPATH'。您可以從終端回顯$ PYTHONPATH,看看它應該如何正確運行腳本,然後在您的crontab中添加一行'PYTHONPATH = ...'來設置它。 –
檢查,... PYTHONPATH確實讓我感到困惑,因爲它在我的終端(bash)中是emtpty,而腳本運行得很好'pi @ raspberrypi〜$ echo $ PYTHONPATH'....編輯這裏沒有新行可見...輸出回聲是沒有什麼 – user3002004