cscho
Posts: 7
Joined: Tue May 31, 2016 3:36 pm

Crontab not working (to log time output into csv file)

Mon Jun 13, 2016 9:08 pm

Hi, I'm trying to loop a python script that logs time into a csv file using crontab; so that it runs on boot

However the crontab line I installed doesn't seem to be working!

here is the python script I want to run every 1 minute:

Code: Select all

#/usr/bin/env python
import sys
import urllib2
import json
import time
import datetime
import os

path = "/home/pi/sources/reader/"

#defining pathway
os.chdir(path)

localtime = time.asctime( time.localtime(time.time()) )

print localtime
with open( 'log3.txt', 'a') as log3 :
    print >>log3, localtime

when I run normally (not on crontab) I get the exact output that I want printed into csv file every minute:

Code: Select all

pi@raspberrypi:~/sources/reader $ sudo python timecrontest.py
Mon Jun 13 21:58:16 2016
This is the crontab command I wrote in the crontab file /tmp/crontab.LmMGAg/crontab

Code: Select all

*/1 * * * * /home/pi/sources/reader/timecrontest.py
How can I make this work? What errors have I made in the code, or is there anything I need to add?
Thank you

User avatar
MarkHaysHarris777
Posts: 1820
Joined: Mon Mar 23, 2015 7:39 am
Location: Rochester, MN
Contact: Website

Re: Crontab not working (to log time output into csv file)

Mon Jun 13, 2016 9:27 pm

You want to think in terms of environments.

... the system probably doesn't know where to find the python code. It is best to put your python script in an .sh file [ #!/usr/bin/python ] in the top line. Have your crontab run the script(s) based on your PATH and PYTHONPATH system variables. Your .sh scripts need to be living in directories that reside in your PATH, and your python imports and .py files need to reside in directories that are in your PYTHONPATH system variable.

Try adjusting your environment...
marcus
:ugeek:

User avatar
scruss
Posts: 3256
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Crontab not working (to log time output into csv file)

Mon Jun 13, 2016 10:11 pm

  1. have you made the script executable? chmod +x ~/sources/reader/timecrontest.py cron won't run it unless it is executable.
  2. is it really in a running crontab? Do crontab -l If it's not listed, it's not gonna run. Use crontab -e to do the editing, don't muck about with the system files.
  3. why are you running it with sudo? It's just logging to the user's own folder. srsly; sudo bad. (But you will need to use sudo once to remove the existing log, since it doesn't belong to user pi).
If you're doing this as a test to see how accurately and often cron fires off jobs, remember it's dependent on system load. It'll run every minute, mostly. Also, since we're multitasking, don't forget that if the job takes longer than a minute to run, cron will happily run multiple copies at once. Hilarity ensues.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

Return to “Troubleshooting”