0

I'm trying to get a ruby script to run via cron. The cron job looks like this:

* * * * * /usr/local/rvm/rubies/ruby-2.0.0-p353/bin/ruby /usr/share/adafruit/webide/repositories/shed_watcher/lib/shed_watcher.rb >> /tmp/cron_shed_watcher.log 2>&1

I've encountered a couple of problems. The first was that the environment cron runs in could find one of the required gems:

require "rest_client"

This was solved by setting the GEM_PATH in the crontab:

GEM_PATH=/usr/local/rvm/gems/ruby-2.0.0-p353:/usr/local/rvm/gems/ruby-2.0.0-p353@global

The second problem is that the script, running on Occidentals on a Raspberry Pi, makes use of a temperature probe and needs to call:

`modprobe w1-gpio`
`modprobe w1-therm`

i.e. modprobe commands in backticks. Cron cannot execute this, instead I get an "operation not permitted" message. modprobe is found in /sbin so I added a path entry to my crontab:

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin/usr/local/rvm/gems/ruby-2.0.0-p353/bin:/usr/local/rvm/gems/ruby-2.0.0-p353@global/bin:....

But I get the same error (not surprising as it was a "not permitted" message, not a "not found" message). What do I need to do to get this working?

2
  • What if you just added your modprobes to rc.local or similar to run it once at boot time. – UVV Jan 12 '14 at 12:06
  • That's a good idea, although I'd prefer the script to not rely on another file – barry Jan 12 '14 at 12:27
1

You should add your job to the root's crontab, not the user's one. User's crontabs always executed with uid/gid of the user when root crontab can contain additional field:

* * * * * username /path/to/the/command

Then command will be executed with username privileges/permissions.

4
  • Thanks, that worked although I didn't need to add a username – barry Jan 12 '14 at 20:32
  • It works as root if no username placed, cause root hasn't restrictions. But for security reasons you have to set appropriate uid - possibly www. – Kondybas Jan 12 '14 at 21:37
  • Hmmm.... I didn't set a uid – barry Jan 13 '14 at 0:08
  • Crontab owner always assumed when username omitted. Root can change to privileges to some other user while nonprivileged users can't. – Kondybas Jan 13 '14 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.