vendredi 29 mai 2015

Cronjob: Python script not writing to file

I'm working on raspbian and wrote a python script which communicates via RS232 with some hardware relatet to pysical IO States of the Raspberry. It also writes to a logfile.

Everything works fine when I start the script from command line: pi@raspberrypi ~/scripts $ python steppercontrol.py

I added the scrip as a 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

The script works and is running after reboot, but the logfile is not written syslog gives the following

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)

The chmods should be ok:

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 it's not related to python itself. I also could not manage to redirect stdout from the script (as cronjob) to a file. I am lost, here is my script:

Btw: it's my first python script and generally i am not very good with linux, but raspbian and google make things easy ;-)

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))

Aucun commentaire:

Enregistrer un commentaire