2

Running latest version of Rasbian and want to execute /home/pi/temp.py from cron every 2 min. Have chmod 755 on the file

When I run the command from the prompt it executes fine and the script does what it supposed to: pi@raspberrypi ~ $ python temp.py

I have added it in cron like: */2 * * * * /usr/bin/python /home/pi/temp.py

I never get any output from the script. I have other scripts the run fine from cron. What do I miss?

This is what temp.py looks like

!/usr/bin/python
import time
 # Create a unique timestamp for inclusion in the log...
timestamp = time.strftime("Time: %H:%M, Date: %Y%m%d, ")
 # append the filename with the date (this will start a new log daily)...
logname = time.strftime("")
filename = "".join(["/home/pi/bredhult_temp.txt"])
 # If already created, this will open the current log file, if not it will create a new on
datafile = open(filename, "a")
 # open the sensor log file as seen in the tutuorial (replacing 28-000003f1cede with the n
tfile = open("/sys/bus/w1/devices/28-000006879f89/w1_slave")
 # read the data and close the file...
text = tfile.read()
tfile.close()
 # Split the data and extract the temperature value...
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature = temperature / 1000
 # write our newly formatted date, time and sensor value to our log file...
datafile.write(timestamp + "Temp: " + str(temperature) + "\n")
datafile.close()
6
  • Where is the output meant to go? If temp.py is doing I/O to a file make sure you are using the full path to the file name.
    – joan
    Commented Mar 26, 2015 at 10:50
  • Added the temp.py code Commented Mar 26, 2015 at 12:52
  • temp.py is missing a # before the /usr/bin/python on line 1. I can't see that making a difference. Was that a cut&paste error?
    – joan
    Commented Mar 26, 2015 at 13:33
  • You didn't say how you added this to the crontab - or whose crontab this is - is it 'root' or 'pi' (or another?)
    – monojohnny
    Commented Mar 26, 2015 at 13:57
  • 2
    What is the purpose of this abomination? filename = "".join(["/home/pi/bredhult_temp.txt"])
    – Bex
    Commented Mar 27, 2015 at 13:16

4 Answers 4

1

SUGGESTION:

First create a simple Python script - that just outputs something to a file.

with open("/tmp/hello.txt", "w") as outfile:
        outfile.write("hello")

You don't need the hashbang (#!/usr/bin/python) here (although there is no harm in adding it if you want) : since you will be using the Python executable to call a script in your cron.

To simplify the cron (and you need to decide whether to use 'root' or 'pi' (or another) crontab here) : I assume you will be using the 'root' cron here - use the @reboot instead of a specific time.

$ sudo crontab -e

Make the change, and save the crontab:

@reboot     /usr/bin/python /home/pi/simple.py

$ sudo reboot

Now check that /tmp contains the 'hello.txt' file after the reboot. If this works: swap out the (crontab -e again) this simple script for your existing one , reboot ) - does this work ?

Note: you can also check your system logs for evidence that the cron is running your script or not:

See this other post for information.

1

Looking at your pasted example, the shebang is missing #; your first line should be:

#!/usr/bin/python

Once that is fixed I would try to make your python script executable with the following line:

chmod +x /home/pi/temp.py

And then simply call it with the following in your crontab:

*/2 * * * * /home/pi/temp.py
0

This is how I run a python program as root every five minutes.

johnma@raspberrypi ~ $ sudo crontab -l
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h  dom mon dow   command
*/5 * * * * /home/johnma/do-DHT-once

Here is the source of do-DHT-once which automatically outputs sysout and syserr to temp files in /tmp and checks to see if a previous copy of the script is still running and tries to kill it if it is. The python program outputs its PID to file /tmp/DHT.pid. The script can also be disabled by creating a file called DHT.disabled in $DIR:

#!/bin/bash

# Set home directory where script exists
DIR=/home/johnma

if [ -f $DIR/DHT.disabled ]
then
  exit 1
fi

if [ -f /tmp/DHT.pid ]
then
  echo Attempting to kill process `cat /tmp/DHT.pid` >>/tmp/DHT.log 2>>/tmp/DHT.err.log
  kill -HUP `cat /tmp/DHT.pid` >>/tmp/DHT.log 2>>/tmp/DHT.err.log
fi

# change to home directory
cd $DIR

# run the program using the debug version of python
python2.7-dbg $DIR/DHT_googledocs.twitter.once.py >>/tmp/DHT.log 2>>/tmp/DHT.err.log

if [ -f /tmp/DHT.pid ]
then
  echo Attempting to delete /tmp/DHT.pid >>/tmp/DHT.log 2>>/tmp/DHT.err.log
  rm /tmp/DHT.pid >>/tmp/DHT.log 2>>/tmp/DHT.err.log
fi

Finally, the file do-DHT-once has permissions 755 to allow it to be executed:

johnma@raspberrypi ~ $ ls -l do-DHT-once
-rwxr--r-- 1 johnma johnma 703 May 12  2014 do-DHT-once
0

I had problems with Python script as cronjob as well. I recommend to write a simple wrapper bash script which sets PYTHONPATH (or whatever) correctly, put it in $HOME/bin and run a script like the following example:

#!/bin/bash

# Optionally set PYTHONPATH
export PYTHONPATH=/path/to/python/libraries:$PYTHONPATH

# Run your script with your preferred Python installation
# in case you have more than one
/path/to/your/python /path/to/yourscript.py

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.