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


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')
print "-Ceilometer Logfile"
time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print "-File created: "+time_now 

  baudrate = 19200,

while 1:

 tdata = ser.read()
 time_now2 = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
 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
  • Am I reading it wrong, or does the program never terminate?
    – Weeble
    Commented Jan 22, 2016 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
    Commented Jan 22, 2016 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? Commented Jan 22, 2016 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
    Commented Jan 22, 2016 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? Commented Jan 22, 2016 at 23:16

1 Answer 1


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.

  • 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? Commented Jan 22, 2016 at 16:52
  • 1
    I'm guessing it just ignores the \5. * alone would be every minute.
    – goldilocks
    Commented Jan 22, 2016 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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