User avatar
some_evil
Posts: 205
Joined: Thu Dec 18, 2014 3:16 am
Location: Albury, Australia

Help with crontab and py script

Wed Mar 11, 2015 10:17 am

Hi guys,

I am trying to automate my first script on my py to run at a specific time, and in my attached example it is trying to run at 8.37pm every day of the year, but the output file of my python script seems to be empty after its scheduled start time.
2015-03-11 21_18_35-BVK - RaspberryPi - WinSCP.jpg
2015-03-11 21_18_35-BVK - RaspberryPi - WinSCP.jpg (23.41 KiB) Viewed 797 times

I can manually start this script by typing:

Code: Select all

sudo python /BVK/ds18.py
When i run the code it outputs the temperature to a text file, but its not producing anything, and im unsure how to look for errors.... any tips on what I can do to find my problem?

Thanks
PiZeroW - May 2017
Pi 3 - Oct 2016
PiZero - June 2016
Pi 2 - Jan 2016
Pi B+ - Dec 25 2014

User avatar
RaTTuS
Posts: 10563
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Help with crontab and py script

Wed Mar 11, 2015 10:31 am

1) dont use sudo in cron
if you need to run it as root use the roots cronttab
i.e.
sudo contab -e
2) always use full paths to everything
.... /full/path/to/command/processor /full/path/to/script
or
... /full/path/to/script
as you will have
#!/full/path/to/processor
in the first line of your script anyway
and the output
as you are not showing on the command line this must be in your script
again have the full path to the output file in there
and I would use a RAM disk location i.e.
/tmp
3) cron will report errors to the syslog
i.e.
grep CRON /var/log/syslog
Last edited by RaTTuS on Wed Mar 11, 2015 11:06 am, edited 1 time in total.
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

User avatar
some_evil
Posts: 205
Joined: Thu Dec 18, 2014 3:16 am
Location: Albury, Australia

Re: Help with crontab and py script

Wed Mar 11, 2015 11:03 am

RaTTuS wrote:1) dont use sudo in cron
if you need to run it as root use the roots cronttab
i.e.
sudo contrab -e
So i should edit it to look like...
37 20 * * * python /BVK/ds18.py ???

Also I am a n00b, but i assume you meant 'sudo crontab -e' ???
RaTTuS wrote:2) always use full paths to everything
.... /full/path/to/command/processor /full/path/to/script
or
... /full/path/to/script
as you will have
#!/full/path/to/processor
in the first line of your script anyway
ok this is good, I have seen this line in scripts from time to time but never knew what it did or how to apply it. Is the "#!/full/path/to/processor" going to be the same for eveyone on a Pi? I dont quite get what this is doing?

I have never used this first line before, but my basic scripts have run (up until this one).
RaTTuS wrote: as you are not showing on the command line this must be in your script
again have the full path to the output file in there
this is the one thing I was able to check without further clarification, i went through my script and realized that I had only put output = 'outputFile.txt', i just changed it to output = '/BVK/outputFile.txt' and it has just run successfully and automatically.
RaTTuS wrote:and I would use a RAM disk location i.e.
/tmp
What would you use this for??


Thanks for your help!
PiZeroW - May 2017
Pi 3 - Oct 2016
PiZero - June 2016
Pi 2 - Jan 2016
Pi B+ - Dec 25 2014

User avatar
RaTTuS
Posts: 10563
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: Help with crontab and py script

Wed Mar 11, 2015 11:11 am

some_evil wrote: Also I am a n00b, but i assume you meant 'sudo crontab -e' ???
yes :oops: and I've edited the original post
RaTTuS wrote:ok this is good, I have seen this line in scripts from time to time but never knew what it did or how to apply it. Is the "#!/full/path/to/processor" going to be the same for eveyone on a Pi? I dont quite get what this is doing?
yes in general -
if people have installed via apt-get then they all should be in the same place, though if it fails you'll get an error

well as you have not posted your script I cannot comment on it as to where you store things

but if you type
mount
then if it shows
something similar to
tmpfs on /tmp type tempfs ( ....)
then that device is a ramdisk
this will save on writes to the SD card [though once a day is nothing] but it also means that it will be cleared on reboots so do don't have random stuff left on the Sdcard
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Help with crontab and py script

Wed Mar 11, 2015 11:12 am

Two things (related):
  1. Check in your syslog, searching for the string "mta" (in any case, not sure which spelling [case] it is in there with). You should see a message like (paraphrasing) "MTA not configured". This message is telling you that your cron job generated something on standard output and/or standard error, and the cron system tried to email that output to you, but couldn't, because you haven't setup your MTA (mail transfer agent).
  2. Fix the above problem, so that the cron system can send you the output of your command; once you do this, it should be obvious what the problem is and thus easy to fix it. There are two ways of fixing the MTA problem - an easy way and a hard way:
    1. Easy way: add a line like: MAILTO=you@yourprovider.com
    2. Hard way: Install/configure/debug something like sendmail/postfix/exim
    If you follow the first method, then whatever output the cron job generates will be mailed to your outside email account (yahoo, gmail, whatever).
Note finally that you can, in theory sort of, solve this problem by doing the usual "exec 2>&1 >> /tmp/logfile.log" hack, but it is better, at this stage of the game, to let the cron system do its thing in emailing you.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

Return to “Python”