Raspberry Pi Stack Exchange is a question and answer site for users and developers of hardware and software for Raspberry Pi. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a problem with my python script. It looks like this:

#!/usr/bin/python

import time
import serial
import datetime
import sys

Y = datetime.datetime.now().strftime('%Y')[3]
filedate = datetime.datetime.now().strftime('Z'+Y+'%m%d%H%M')
sys.stdout=open(filedate+".dat","w")
#sys.stdout=open("test.dat","w")
print "-Ceilometer Logfile"
time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print "-File created: "+time_now 
ser=serial.Serial(

  port='/dev/ttyUSB0',
  baudrate = 19200,
  parity=serial.PARITY_NONE,
  stopbits=serial.STOPBITS_ONE,
  bytesize=serial.EIGHTBITS,
)
counter=0

ser.flushInput()
while 1:

 tdata = ser.read()
 time_now2 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
 time.sleep(3)
 data_left = ser.inWaiting()
 tdata += ser.read(data_left)

 print "-"+time_now2,"\n",
 print tdata

It reads the input coming from a serial port using the USB port on the Pi and a serial to USB adapter. There are two issues:

1) The script works fine once started. When I cancel it and start it again there is no error, but the output file is completely messed up. The timestamp is randomly somewhere, there is data missing and there are random digits or character printed in the file. Any what can cause this? My guess is an issue with the serial buffer?!

2) I tried to execute the program every 5 minutes (for test purposes, normally it would be once an hour). But the program created an output file every minute instead of every five minutes. And all the files were messes up as said earlier. Even though everything was fine before that test.

The cronjob looks like this:

*\5 * * * * sudo python /home/pi/readserial.py
share|improve this question
    
Am I reading it wrong, or does the program never terminate? – Weeble Jan 22 at 20:11
    
"When I cancel it and start it again" - how exactly are you starting and cancelling it? Is it possible that the first instance is still running? That could probably explain the observed problems. – Weeble Jan 22 at 20:17
    
Correct, the program never terminates automatically, because it needs to listen all the time. I start it using 'sudo python script.py' and end it pressing 'ctrl+c'. If I get the cronjob function right, the cron deamon kills the old instance before starting a new one each hour? Or do I end up with trillions of started scripts? – BallerNacken Jan 22 at 21:07
1  
cron does not kill any existing process before starting a job. If your program runs forever you will have an ever increasing number of processes running. – Craig Jan 22 at 22:22
    
So what would be the better way to have the program listen on the serial port all the time but creating a file each hour? Put into init.d? – BallerNacken Jan 22 at 23:16

From man 5 crontab:

Step values can be used in conjunction with ranges. Following a range with "/<number>" specifies skips of the number's value through the range. For example, "0-23/2" can be used in the 'hours' field to specify command execution for every other hour (the alternative in the V7 standard is "0,2,4,6,8,10,12,14,16,18,20,22"). Step values are also permitted after an asterisk, so if specifying a job to be run every two hours, you can use "*/2".

You are using a backslash, but the spec is a forward slash.

The reason to look in section 5 of the manual (man 5 ...) is that crontab is command, and a related type of configuration file. Commands are in section 1, files are in section 5. man all by itself defaults to section 1, so just plain man crontab does not give you the page with this information.

share|improve this answer
    
OMG I am really good at making such stupid mistakes. Will change it on Monday. But why is it then running the script every minute? Is that the default or why a file every minute? – BallerNacken Jan 22 at 16:52
1  
I'm guessing it just ignores the \5. * alone would be every minute. – goldilocks Jan 22 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.