Page 1 of 1

need help running root crontab

Posted: Sun Oct 27, 2013 7:27 pm
by Ecurb
Hello all,

I'm trying to use the crontab method to start my python script like in this thread:
http://www.raspberrypi-spy.co.uk/2013/0 ... sing-cron/

I did follow the steps and the script does run, however it does not work with the GPIO pins which need the root access. The script runs fine if I log in as root and run it in the terminal. So my thinking is that the crontab is not running with the root users. when I log in as root and open the crontab it says "File: /tmp/crontab.KvaEFW/crontab" at the top. Is that correct? Where is the one for root and how would I edit it?

Thanks,
Bruce

Re: need help running root crontab

Posted: Sun Oct 27, 2013 7:38 pm
by DeeJay
The author of that tutorial is usually reliable, and a frequent contributor to these forums. He has told you how to edit the root crontab - only he says "we want to add it as an admin" instead of referring to 'root'.

How do you know the script is not working? If it is because you are not seeing output on the screen, that might not be a useful diagnostic... Can you get the script to write a message to a named logfile instead, at least for debugging purposes?

Re: need help running root crontab

Posted: Sun Oct 27, 2013 8:18 pm
by ragnarjensen
Ecurb wrote:when I log in as root and open the crontab it says "File: /tmp/crontab.KvaEFW/crontab" at the top. Is that correct?
Yes, that is correct. The crontab -e command makes a temporary copy of the existing file and edits that.
When you exit the editor and save your changes the temporary file is copied back to replace the old one.
Ecurb wrote:Where is the one for root and how would I edit it?
Crontabs are kept in /var/spool/cron/crontabs
You shouldn't edit them directly, use the crontab -e command.

Re: need help running root crontab

Posted: Sun Oct 27, 2013 8:30 pm
by DougieLawson
If your python script includes any "print" statements when crontab runs it then that printed output is sent by email.

Use `crontab -l` to list entries. Use `crontab -e` to edit your crontab (and refresh it when you save changes).

Also don't forget to set the PATH or use full names for anything you're calling external to python (if it's not a bash built-in command), assume that you have a bare environment which is only running /bin/sh when crontab runs your script. For python2 I created this simple python program

Code: Select all

#! /usr/bin/python

import sys
print sys.path
And got this result when I ran it as a cron job

Code: Select all

['/root/picode',
'/usr/local/lib/python2.7/dist-packages/quick2wire_api-0.0.0.3-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/RPi.GPIO-0.1.0-py2.7.egg',
'/usr/local/lib/python2.7/dist-packages/nanpy-0.8-py2.7.egg',
'/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2',
'/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old',
'/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages',
'/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7']

Note: I had to install procmail & postfix to get that working.

Re: need help running root crontab

Posted: Sun Oct 27, 2013 10:09 pm
by Ecurb
Thanks all for the ideas, I'll get back to work on it tomorrow after work!

Bruce

Re: need help running root crontab

Posted: Fri Nov 01, 2013 11:01 pm
by Ecurb
Hi all,

Just as a final update incase anyone finds this thread in a search one day. I never got crontab to work for me, I ended up using the rc.local file in the init.d folder. The rc.local which was in the /etc/ folder did not work for me.

Bruce