我正在使用raspbian并编写了一个python脚本,该脚本通过RS232与一些与Raspberry的物理IO状态相关的硬件进行通信。它还会写入日志文件。
从命令行启动脚本时,一切正常:
pi@raspberrypi ~/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
脚本工作并在重新启动后运行,但不写入日志文件 syslog提供以下内容
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应该没问题:
pi@raspberrypi ~/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本身无关。我也无法将stdout从脚本(作为cronjob)重定向到文件。我迷路了,这是我的剧本:
Btw:这是我的第一个python脚本,一般来说我对linux不是很好,但是raspbian和google让事情变得简单; - )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 :(得分:0)
您的cron作业正在向stdout或stderr写入内容 - cron收集并尝试将其邮寄给您,但您的系统未安装MTA(因此日志消息“未安装MTA”)。尝试将stderr和stdout重定向到文件,您可能会获得有关问题的更多信息。
或者,您可以安装MTA并让它向您发送输出 - 这将帮助您解决将来遇到的任何其他问题:
sudo apt-get install postfix
答案 1 :(得分:0)
最终答案显而易见! 我没有刷新输出。对于stdout,stderr:
import sys
sys.stdout.flush()
sys.stderr.flush()
打开文件:
logfile.flush()