Ecurb
Posts: 25
Joined: Sun Oct 06, 2013 6:31 pm

need help running root crontab

Sun Oct 27, 2013 7:27 pm

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

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: need help running root crontab

Sun Oct 27, 2013 7:38 pm

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?
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

User avatar
ragnarjensen
Posts: 332
Joined: Wed May 15, 2013 6:13 pm
Location: Stockholm, Sweden
Contact: Website

Re: need help running root crontab

Sun Oct 27, 2013 8:18 pm

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.

User avatar
DougieLawson
Posts: 36572
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: need help running root crontab

Sun Oct 27, 2013 8:30 pm

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.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Ecurb
Posts: 25
Joined: Sun Oct 06, 2013 6:31 pm

Re: need help running root crontab

Sun Oct 27, 2013 10:09 pm

Thanks all for the ideas, I'll get back to work on it tomorrow after work!

Bruce

Ecurb
Posts: 25
Joined: Sun Oct 06, 2013 6:31 pm

Re: need help running root crontab

Fri Nov 01, 2013 11:01 pm

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

Return to “Troubleshooting”