Cronjob:Python 脚本未写入文件

Cronjob: Python script not writing to file

我正在研究 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

脚本有效,重启后 运行,但未写入日志文件 系统日志给出以下内容

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)

chmod 应该没问题:

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 本身无关。我也无法设法将标准输出从脚本(作为 cronjob)重定向到文件。我迷路了,这是我的脚本:

顺便说一句:这是我的第一个 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))

您的 cron 作业正在向 stdout 或 stderr 写入内容 - cron 收集并尝试将其邮寄给您,但您的系统未安装 MTA(因此出现日志消息 "No MTA installed")。尝试将 stderr 和 stdout 重定向到一个文件,您可能会获得有关问题所在的更多信息。

或者,您可以安装 MTA 并让它向您发送输出 - 这将帮助您解决将来在 cron 任务中遇到的任何其他问题:

sudo apt-get install postfix

最终答案显而易见! 我没有刷新输出。对于标准输出,标准错误:

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

打开的文件:

logfile.flush()