Forum: Ruby Problem with cronjob and ruby script

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2b34bebcea5708f5d5b49cdeca72c726?d=identicon&s=25 Matthew Lagace (rasibm)
on 2009-06-05 15:48
Hello,

I'm trying to get a ruby script run into a crontab and it keeps failing
saying it can't find my mechanize gem. When I run the script form the
command line, everything runs fine and I don't get any failures.

Anyone ever had that problem before?
60c6b87c4cc2716c83a737e0ba2d3bc0?d=identicon&s=25 David Rio (Guest)
on 2009-06-05 16:26
(Received via mailing list)
On Fri, Jun 5, 2009 at 3:48 PM, Matthew Lagace <mlagace@rasib.ca> wrote:
> Hello,
>
> I'm trying to get a ruby script run into a crontab and it keeps failing
> saying it can't find my mechanize gem. When I run the script form the
> command line, everything runs fine and I don't get any failures.
>
> Anyone ever had that problem before?

Make sure you have this ENV variables pointing to the correct dirs:

GEM_HOME=/usr/local/rubygems/gems
GEM_PATH=/usr/local/rubygems/gems
RUBYLIB=/usr/local/rubygems/lib/ruby/site_ruby/1.8

-drd
C2640526001e7aac8d1f696b5e3feb9d?d=identicon&s=25 Eddie Corns (Guest)
on 2009-06-05 16:36
(Received via mailing list)
Matthew Lagace <mlagace@rasib.ca> writes:

>Hello,

>I'm trying to get a ruby script run into a crontab and it keeps failing
>saying it can't find my mechanize gem. When I run the script form the
>command line, everything runs fine and I don't get any failures.

Often and often when cron jobs fail it is because the environment
variables
are not set.  Cron jobs do not automatically execute .bashrc (or
equivalent)
you generally need to create a script that calls your program after
either
setting the environment by hand or doing eg ". .bashrc".

Eddie
A87f7a014c624587fab0d3d78c5b9c18?d=identicon&s=25 Bil Kleb (Guest)
on 2009-06-05 16:47
(Received via mailing list)
Matthew Lagace wrote:
> Hello,

Hi.

> I'm trying to get a ruby script run into a crontab and it keeps failing
> saying it can't find my mechanize gem. When I run the script form the
> command line, everything runs fine and I don't get any failures.
>
> Anyone ever had that problem before?

Oh yes.

Try getting it to run from the command line with a blank
environment with 'env -', e.g.,

  % env - run_stuff.sh

Typically a crontab job runs with a much reduced environment.

Alternatively, stick a printenv in your crontab to see
what environment it is working with and compare to your
interactive environment.

You might also find crontab's sister command, 'at', helpful
to get tighter feedback cycles.

Regards,
403b1743ad9522eeb22f301727ffc714?d=identicon&s=25 James French (Guest)
on 2009-06-05 16:52
(Received via mailing list)
Hi,

If I start by a human-written block of data, such as ITEMS below, is it
possible to deserialize these into objects of type Item? How might this
be done?

Thanks for any help,
James


ITEMS = %q{
---
item:
 - name:      blah
 - type:      blah
 - location:  blah
}

class Item

  attr_accessor :name
  attr_accessor :type
  attr_accessor :location

  def initialize
    @name = ""
    @type = ""
    @location = ""
  end

end
2b34bebcea5708f5d5b49cdeca72c726?d=identicon&s=25 Matthew Lagace (rasibm)
on 2009-06-05 17:00
Hmm I've tried running with a blank environment and it fails with the
same error not finding the gems. I have set the ENV variables to the
proper directories and it is still giving me the same error. I'm not
sure what else to try although I appreciate all your recommendations!



Bil Kleb wrote:
> Matthew Lagace wrote:
>> Hello,
>
> Hi.
>
>> I'm trying to get a ruby script run into a crontab and it keeps failing
>> saying it can't find my mechanize gem. When I run the script form the
>> command line, everything runs fine and I don't get any failures.
>>
>> Anyone ever had that problem before?
>
> Oh yes.
>
> Try getting it to run from the command line with a blank
> environment with 'env -', e.g.,
>
>   % env - run_stuff.sh
>
> Typically a crontab job runs with a much reduced environment.
>
> Alternatively, stick a printenv in your crontab to see
> what environment it is working with and compare to your
> interactive environment.
>
> You might also find crontab's sister command, 'at', helpful
> to get tighter feedback cycles.
>
> Regards,
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-06-05 17:24
(Received via mailing list)
On Fri, Jun 5, 2009 at 8:00 AM, Matthew Lagace<mlagace@rasib.ca> wrote:
> Hmm I've tried running with a blank environment and it fails with the
> same error not finding the gems. I have set the ENV variables to the
> proper directories and it is still giving me the same error.

Set them *where*? You need to set them in your crontab directly,
*not* in your personal e.g. .bashrc.
2b34bebcea5708f5d5b49cdeca72c726?d=identicon&s=25 Matthew Lagace (rasibm)
on 2009-06-05 17:36
Oh I see can you give me an example please?

Thanks!

Hassan Schroeder wrote:
> On Fri, Jun 5, 2009 at 8:00 AM, Matthew Lagace<mlagace@rasib.ca> wrote:
>> Hmm I've tried running with a blank environment and it fails with the
>> same error not finding the gems. I have set the ENV variables to the
>> proper directories and it is still giving me the same error.
>
> Set them *where*? You need to set them in your crontab directly,
> *not* in your personal e.g. .bashrc.
Cd0dbf751457cf6fed1f69070d37006a?d=identicon&s=25 Charles Johnson (Guest)
on 2009-06-05 17:55
(Received via mailing list)
On Jun 5, 2009, at 10:36 AM, Matthew Lagace wrote:

> Oh I see can you give me an example please?
>
> Thanks!
>
You may find this helpful: http://blog.spikesource.com/crontab.htm

Cheers--

Charles
---
Charles Johnson
Advanced Computing Center for Research and Education
Vanderbilt University
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2009-06-05 17:57
(Received via mailing list)
On Fri, Jun 5, 2009 at 10:36 AM, Matthew Lagace<mlagace@rasib.ca> wrote:
> Oh I see can you give me an example please?
>
> Thanks!


From (recent) experience here, I've had similar issues across
different versions of the same distro (CentOS/RedHat).  The paths and
environment variables are drastically different.  For me, I was
shelling out to lspci

Something along the lines of this would have been a valid fix.

#Crontab
0 1 * * * PATH=$PATH:/sbin /usr/bin/ruby /usr/local/bin/nitejob.rb
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-06-05 18:14
(Received via mailing list)
On Fri, Jun 5, 2009 at 8:36 AM, Matthew Lagace<mlagace@rasib.ca> wrote:
> Oh I see can you give me an example please?

Sure, here's one:

5,20,35,50 * * * * export JAVA_HOME=/usr/local/jdk1.6_013  &&
/usr/bin/jruby - S blah blah

HTH,
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2009-06-05 18:39
(Received via mailing list)
On Fri, Jun 5, 2009 at 11:13 AM, Hassan
Schroeder<hassan.schroeder@gmail.com> wrote:
> Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
Hassan, just a note, you don't need the "export" part of the line :)

--Kyle
9dec3df8319c613f6f4f14a27da0fdb4?d=identicon&s=25 Kyle Schmitt (Guest)
on 2009-06-05 18:39
(Received via mailing list)
On Fri, Jun 5, 2009 at 11:38 AM, Kyle Schmitt<kyleaschmitt@gmail.com>
wrote:
>
Of course it doesn't hurt either....
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-06-05 19:10
(Received via mailing list)
On Fri, Jun 5, 2009 at 9:38 AM, Kyle Schmitt<kyleaschmitt@gmail.com>
wrote:

>> 5,20,35,50 * * * * export JAVA_HOME=/usr/local/jdk1.6_013  &&
>> /usr/bin/jruby - S blah blah

> Hassan, just a note, you don't need the "export" part of the line :)

Force of habit; in this example, no, but if a subsequent part of the
entry spawns a new shell, I think it's necessary.

I think. Will have to revisit that sometime, but not today  :-)
2b34bebcea5708f5d5b49cdeca72c726?d=identicon&s=25 Matthew Lagace (rasibm)
on 2009-06-05 20:19
Tried everything in here and same problem. What I've noticed though this
version of Ubuntu (Feisty Fawn 7.04) which I know is outdated messed up
all the paths for ruby and rubygems.

Hassan Schroeder wrote:
> On Fri, Jun 5, 2009 at 9:38 AM, Kyle Schmitt<kyleaschmitt@gmail.com>
> wrote:
>
>>> 5,20,35,50 * * * * export JAVA_HOME=/usr/local/jdk1.6_013 �&&
>>> /usr/bin/jruby - S blah blah
>
>> Hassan, just a note, you don't need the "export" part of the line :)
>
> Force of habit; in this example, no, but if a subsequent part of the
> entry spawns a new shell, I think it's necessary.
>
> I think. Will have to revisit that sometime, but not today  :-)
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-06-05 20:26
(Received via mailing list)
On Fri, Jun 5, 2009 at 11:19 AM, Matthew Lagace<mlagace@rasib.ca> wrote:
> Tried everything in here and same problem. What I've noticed though this
> version of Ubuntu (Feisty Fawn 7.04) which I know is outdated messed up
> all the paths for ruby and rubygems.

Shouldn't matter if you put in full paths to everything you reference.
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2009-06-05 20:43
(Received via mailing list)
James French wrote:
> Hi,
>
> If I start by a human-written block of data, such as ITEMS below, is it possible to 
deserialize these into objects of type Item? How might this be done?

Easy, just yaml them to find out how the serialized form should look:

   class Item

     attr_accessor :name
     attr_accessor :type
     attr_accessor :location

     def initialize
       @name = ""
       @type = ""
       @location = ""
     end

   end

   require 'yaml'
   y Item.new


If you run this you get:

--- !ruby/object:Item
location: ""
name: ""
type: ""


Then you can play with the string and check that YAML.load does the
right thing on this data
8e44b211865cd5915f2f883fd960b56d?d=identicon&s=25 Xeno Campanoli (Guest)
on 2009-06-05 21:38
(Received via mailing list)
Matthew Lagace wrote:
>>> Hassan, just a note, you don't need the "export" part of the line :)
>> Force of habit; in this example, no, but if a subsequent part of the
>> entry spawns a new shell, I think it's necessary.
>>
>> I think. Will have to revisit that sometime, but not today  :-)
>

Hey, does this thread have anything to do with the Bourne-shell usage
bias which
first manifested itself for me in at last year?  In other words, if you
use a
shell, any shell, in at you get sh, not bash, the former being
considered
lighter weight and perhaps somehow safer and more standard, but on the
other
hand breaks things like some commands you are used to in some syntax in
Bash.
For instance, you must do the following to export:

x=y
export x

because export x=y does not work in sh.  ???
2b34bebcea5708f5d5b49cdeca72c726?d=identicon&s=25 Matthew Lagace (rasibm)
on 2009-06-05 21:42
I honestly don't know, I've tried everything in here and I still can't
get it to work.

Hassan Schroeder wrote:
> On Fri, Jun 5, 2009 at 11:19 AM, Matthew Lagace<mlagace@rasib.ca> wrote:
>> Tried everything in here and same problem. What I've noticed though this
>> version of Ubuntu (Feisty Fawn 7.04) which I know is outdated messed up
>> all the paths for ruby and rubygems.
>
> Shouldn't matter if you put in full paths to everything you reference.
Bee69cfed999cd13e3bff73d472a39ee?d=identicon&s=25 Hassan Schroeder (Guest)
on 2009-06-05 22:55
(Received via mailing list)
On Fri, Jun 5, 2009 at 12:42 PM, Matthew Lagace<mlagace@rasib.ca> wrote:
> I honestly don't know, I've tried everything in here and I still can't
> get it to work.

Maybe if you post the actual crontab entry that you're having trouble
with, we'd have a chance of finding the specific problem  :-)
This topic is locked and can not be replied to.