Crobjob problem with ruby script.

Discussion in 'Ruby' started by andreas.cahen@gmail.com, Mar 18, 2005.

  1. Guest

    Hi!

    Hopefully this is the right place to post my problem.. any help is
    appreciated!

    I am using two Ruby scripts on my *inux server to purge old records out
    of a mysql database. These two scripts run, or should, as cronjobs. If
    I start those scripts directly from the shell it all works fine. But as
    a cronjob I get the following error:

    /backups/scripts/delete_old.rb:1:in `require': No such file to load --
    dbi (LoadError)
    from /backups/scripts/delete_old.rb:1

    The command for the cron job is: ruby /backups/scripts/delete_old.rb

    Where did I go wrong? :) Are there any environement variables which I
    forgot to set?

    Thanks!!

    -Andreas
     
    , Mar 18, 2005
    #1
    1. Advertising

  2. James Britt Guest

    wrote:
    > Hi!
    >
    > Hopefully this is the right place to post my problem.. any help is
    > appreciated!
    >
    > I am using two Ruby scripts on my *inux server to purge old records out
    > of a mysql database. These two scripts run, or should, as cronjobs. If
    > I start those scripts directly from the shell it all works fine. But as
    > a cronjob I get the following error:
    >
    > /backups/scripts/delete_old.rb:1:in `require': No such file to load --
    > dbi (LoadError)
    > from /backups/scripts/delete_old.rb:1
    >
    > The command for the cron job is: ruby /backups/scripts/delete_old.rb
    >
    > Where did I go wrong? :) Are there any environement variables which I
    > forgot to set?


    What shebang line are you using?

    What user account is executing the code?


    >
    > Thanks!!
    >
    > -Andreas
    >
    >
    > .
    >


    James Britt
    --


    http://www.ruby-doc.org
    http://www.rubyxml.com
    http://catapult.rubyforge.com
    http://orbjson.rubyforge.com
    http://www.jamesbritt.com
     
    James Britt, Mar 18, 2005
    #2
    1. Advertising

  3. Guest

    James Britt wrote:
    >
    > What shebang line are you using?

    None... do I need to add one to my ruby scripts?

    > What user account is executing the code?

    root is executing the scripts

    -andreas
     
    , Mar 18, 2005
    #3
  4. <> schrieb im Newsbeitrag
    news:...
    > James Britt wrote:
    > >
    > > What shebang line are you using?

    > None... do I need to add one to my ruby scripts?
    >
    > > What user account is executing the code?

    > root is executing the scripts
    >
    > -andreas


    You probably should set $LOAD_PATH explicitely in the script. Try
    ruby -e 'p $LOAD_PATH'
    and see what it prints from the command line and from cron.

    Kind regards

    robert
     
    Robert Klemme, Mar 18, 2005
    #4
  5. James Britt Guest

    Robert Klemme wrote:
    > <> schrieb im Newsbeitrag
    > news:...
    >
    >>James Britt wrote:
    >>
    >>>What shebang line are you using?

    >>
    >>None... do I need to add one to my ruby scripts?
    >>
    >>
    >>>What user account is executing the code?

    >>
    >>root is executing the scripts
    >>
    >>-andreas

    >
    >
    > You probably should set $LOAD_PATH explicitely in the script. Try
    > ruby -e 'p $LOAD_PATH'
    > and see what it prints from the command line and from cron.


    I've has issues with cron jobs because of assorted mismatched
    user/path/env details.

    For example, I tried code that began with #!/usr/bin/env ruby
    and this failed because that relies on a given environment setting.

    So, perhaps set a shebang line that points right to ruby.
    Also, make sure that the $LOAD_PATH is not relying on any special
    environment settings (though I've net seen this as an issue myself).

    I believe I've had issues with cron job code making false assumptions
    about the current directory and relative paths.

    Robert's suggestion is a really good one. Ive also done that with a
    script to loop over ENV just to see what the whole environment was.

    And are you *sure* these need to run as root? One side-effect I have
    seen as a result of root cron jobs is that I get files I cannot edit or
    delete without becoming root myself.

    James Britt
     
    James Britt, Mar 18, 2005
    #5
  6. Guest

    Robert Klemme wrote:
    >
    > You probably should set $LOAD_PATH explicitely in the script. Try
    > ruby -e 'p $LOAD_PATH'
    > and see what it prints from the command line and from cron.
    >


    Shell: 375 Bytes
    Cron: 159 Bytes

    I get a huge difference between executing this command on the shell and
    as cronjob.. how comes?

    How do I set the $LOAD_PATH? I tried to set it inside my ruby script
    but I summoned an error..

    Cheers,

    -Andreas
     
    , Mar 18, 2005
    #6
  7. On Sat, 19 Mar 2005 02:59:52 +0900,
    <> wrote:
    >
    > Robert Klemme wrote:
    > >
    > > You probably should set $LOAD_PATH explicitely in the script. Try
    > > ruby -e 'p $LOAD_PATH'
    > > and see what it prints from the command line and from cron.
    > >

    >
    > Shell: 375 Bytes
    > Cron: 159 Bytes
    >
    > I get a huge difference between executing this command on the shell and
    > as cronjob.. how comes?
    >
    > How do I set the $LOAD_PATH? I tried to set it inside my ruby script
    > but I summoned an error..
    >
    > Cheers,
    >
    > -Andreas
    >
    >




    I've had problems with paths in cronjobs even just running shell
    scripts. What I recall mostly is that the current directory was
    something useless, and "~" was not the home directory. I made
    everything work by using all absolute pathes. I don't think that
    would be desirable for a require statement, but I expect it would make
    it work.

    (I don't actually know how require finds the files; I'm guessing
    that's what $LOAD_PATH is for?)

    - Shad






    --

    ----------

    Please do not send personal (non-list-related) mail to this address.
    Personal mail should be sent to .
     
    Shad Sterling, Mar 18, 2005
    #7
  8. Aredridel Guest

    > I've had problems with paths in cronjobs even just running shell
    > scripts. What I recall mostly is that the current directory was
    > something useless, and "~" was not the home directory. I made
    > everything work by using all absolute pathes. I don't think that
    > would be desirable for a require statement, but I expect it would make
    > it work.


    As have I. I am routinely annoyed by the lack of environment
    initialization under most cron daemons.

    > (I don't actually know how require finds the files; I'm guessing
    > that's what $LOAD_PATH is for?)


    Yeah, or its shorthand $:

    Thankfully that's not in the process environment.
     
    Aredridel, Mar 18, 2005
    #8
  9. On Sat, 19 Mar 2005 03:51:04 +0900, Aredridel <> wrote:
    > > I've had problems with paths in cronjobs even just running shell
    > > scripts. What I recall mostly is that the current directory was
    > > something useless, and "~" was not the home directory. I made
    > > everything work by using all absolute pathes. I don't think that
    > > would be desirable for a require statement, but I expect it would make
    > > it work.

    >
    > As have I. I am routinely annoyed by the lack of environment
    > initialization under most cron daemons.
    >



    Most cron daemons? Is there one that doesn't have that problem?


    - Shad



    > > (I don't actually know how require finds the files; I'm guessing
    > > that's what $LOAD_PATH is for?)

    >
    > Yeah, or its shorthand $:
    >
    > Thankfully that's not in the process environment.
    >
    >



    --

    ----------

    Please do not send personal (non-list-related) mail to this address.
    Personal mail should be sent to .
     
    Shad Sterling, Mar 18, 2005
    #9
  10. On 18.03.05,23:24, wrote:
    > Hi!
    >
    > Hopefully this is the right place to post my problem.. any help is
    > appreciated!
    >
    > I am using two Ruby scripts on my *inux server to purge old records out
    > of a mysql database. These two scripts run, or should, as cronjobs. If
    > I start those scripts directly from the shell it all works fine. But as
    > a cronjob I get the following error:
    >
    > /backups/scripts/delete_old.rb:1:in `require': No such file to load --
    > dbi (LoadError)
    > from /backups/scripts/delete_old.rb:1
    >
    > The command for the cron job is: ruby /backups/scripts/delete_old.rb
    >
    > Where did I go wrong? :) Are there any environement variables which I
    > forgot to set?
    >


    Try this instead in the crontab file:

    /usr/bin/ruby /backups/scripts/delete_old.rb

    crontab wants the full path to the ruby executable.


    - Jostein


    --
    Jostein Berntsen <>
     
    Jostein Berntsen, Mar 18, 2005
    #10
  11. Guest

    Hi..

    I tried it like that but it didn't work :/

    Oh my gosh... ruby is powerful but tricky :)

    -Andreas

    Jostein Berntsen wrote:
    >
    > Try this instead in the crontab file:
    >
    > /usr/bin/ruby /backups/scripts/delete_old.rb
    >
    > crontab wants the full path to the ruby executable.
    >
     
    , Mar 19, 2005
    #11
  12. On Sat, 19 Mar 2005 17:34:51 +0900,
    <> wrote:
    > Hi..
    >
    > I tried it like that but it didn't work :/
    >
    > Oh my gosh... ruby is powerful but tricky :)
    >
    > -Andreas
    >
    > Jostein Berntsen wrote:
    > >
    > > Try this instead in the crontab file:
    > >
    > > /usr/bin/ruby /backups/scripts/delete_old.rb


    Try putting that line in a shell script, and add sh
    path/to/shell/script to crontab.

    > >
    > > crontab wants the full path to the ruby executable.
    > >

    >
    >



    --
    Premshree Pillai
    http://www.livejournal.com/users/premshree/
     
    Premshree Pillai, Mar 19, 2005
    #12
  13. Mark Probert Guest

    Hi ..

    On Friday 18 March 2005 06:24, wrote:
    > Hi!
    >
    > I am using two Ruby scripts on my *inux server to purge old records out
    > of a mysql database. These two scripts run, or should, as cronjobs. If
    > I start those scripts directly from the shell it all works fine. But as
    > a cronjob I get the following error:
    >

    My experience with root cronjobs and ruby says that you should explicitly
    define everything. For example, one script, an automated collection agent,
    goes like this:

    $ cat cron_collect
    #! /usr/bin/ksh
    cd /usr/local/share/collect
    /usr/local/share/collect/bin/collect.rb -n input.yaml

    Where 'collect.rb' is a Ruby script with the shebang line

    #! /usr/local/bin/ruby

    and 'input.yaml' is in /usr/loca/share/collect

    The crontab entry looks like

    30 0-23 * * * /usr/local/share/collect/cron_collect

    The shebang line for the executing shell is also important. On a linux box,
    using /usr/bin/bash would probably be better.

    I hope that this helps.

    Regards,

    --
    -mark. (probertm at acm dot org)
     
    Mark Probert, Mar 19, 2005
    #13
  14. Guest

    Unfortunately it didn't help at all..

    Well.. this is my crontab entry
    # 0 0,6,12,18 * * * /backups/scripts/batch_delete.sh

    This is the batch_delete.sh
    #! /bin/bash
    /usr/bin/ruby /backups/scripts/delete_old.rb
    /usr/bin/ruby /backups/scripts/spn_delete_old.rb

    and this is delete_old.rb
    #!/usr/bin/ruby
    require 'dbi'
    require 'pp'
    require 'date'
    values = {}
    count = 0
    id = ''
    id2 = ''
    DBI.connect('DBI:Mysql:test', 'test', 'test') do | dbh |
    puts "selecting..."
    dbh.select_all("SELECT ID FROM `tb_replication_shipment` WHERE
    `createdTimeStamp` < date_sub( now( ) , INTERVAL 30 DAY)") do | row
    |
    count = count + 1
    id = row[0]
    dbh.do("delete FROM tb_replication_shipmentHistory WHERE
    ID_Shipment = #{id}")
    dbh.do("delete from tb_replication_shipment WHERE ID = #{id}")
    end
    puts "#{count} entries were deleted from the system"
    dbh.do("optimize table tb_replication_shipment,
    tb_replication_shipmentHistory")
    end


    I don't get it why it's not working as a cronjob and then again it's
    working fine when executed from the shell.. :/

    thanks,

    -Andreas
     
    , Mar 20, 2005
    #14
  15. <> schrieb im Newsbeitrag
    news:...
    > Unfortunately it didn't help at all..
    >
    > Well.. this is my crontab entry
    > # 0 0,6,12,18 * * * /backups/scripts/batch_delete.sh
    >
    > This is the batch_delete.sh
    > #! /bin/bash
    > /usr/bin/ruby /backups/scripts/delete_old.rb
    > /usr/bin/ruby /backups/scripts/spn_delete_old.rb
    >
    > and this is delete_old.rb
    > #!/usr/bin/ruby
    > require 'dbi'
    > require 'pp'
    > require 'date'
    > values = {}
    > count = 0
    > id = ''
    > id2 = ''
    > DBI.connect('DBI:Mysql:test', 'test', 'test') do | dbh |
    > puts "selecting..."
    > dbh.select_all("SELECT ID FROM `tb_replication_shipment` WHERE
    > `createdTimeStamp` < date_sub( now( ) , INTERVAL 30 DAY)") do | row
    > |
    > count = count + 1
    > id = row[0]
    > dbh.do("delete FROM tb_replication_shipmentHistory WHERE
    > ID_Shipment = #{id}")
    > dbh.do("delete from tb_replication_shipment WHERE ID = #{id}")
    > end
    > puts "#{count} entries were deleted from the system"
    > dbh.do("optimize table tb_replication_shipment,
    > tb_replication_shipmentHistory")
    > end
    >
    >
    > I don't get it why it's not working as a cronjob and then again it's
    > working fine when executed from the shell.. :/
    >
    > thanks,
    >
    > -Andreas


    Try doing this on the shell and set $LOAD_PATH accordingly before the first
    require:

    ruby -e 'puts $LOAD_PATH.join(":")'

    Alternatively you might have a permissions problem but I think path is more
    likely.

    Kind regards

    robert
     
    Robert Klemme, Mar 20, 2005
    #15
  16. Guest

    Robert Klemme wrote:
    > Try doing this on the shell and set $LOAD_PATH accordingly before the

    first
    > require:
    >
    > ruby -e 'puts $LOAD_PATH.join(":")'
    >
    > Alternatively you might have a permissions problem but I think path

    is more
    > likely.
    >
    > Kind regards
    >
    > robert


    Good Evening Robert!

    Thanks for supporting me with my silly but serious problem.. :)

    I have spotted a big difference between the $LOAD_PATH of the shell and
    executing the same from the cronjob. How can I set the correct
    $LOAD_PATH inside the rubyscript?

    $LOAD_PATH of the shell:
    /usr/local/lib/ruby/site_ruby:/usr/local/lib/ruby/site_ruby/1.8:/usr/local/lib/ruby/1.8/:/usr/local/lib/ruby/site_ruby/1.8/i686-linux/:/usr/local/lib/ruby/1.8/i686-linux/:/usr/lib/ruby/site_ruby/1.8:/usr/lib/ruby/site_ruby/1.8/i686-linux:/usr/lib/ruby/site_ruby:/usr/lib/ruby/1.8:/usr/lib/ruby/1.8/i686-linux:.

    $LOAD_PATH of the cronjob:
    /usr/lib/ruby/site_ruby/1.8:/usr/lib/ruby/site_ruby/1.8/i686-linux:/usr/lib/ruby/site_ruby:/usr/lib/ruby/1.8:/usr/lib/ruby/1.8/i686-linux:.

    Now that's a difference.. :)

    Any suggestions on how I may add the missing paths?

    regards,

    Andreas
     
    , Mar 20, 2005
    #16
  17. Guest

    I finally found out how to use my rubyscripts as a cronjob..
    Thanks to all you guys for helping me!!

    This is how I made it..

    ---[batch_delete.sh]---
    #! /bin/bash
    /usr/bin/ruby -I
    /usr/local/lib/ruby/site_ruby:/usr/local/lib/ruby/site_ruby/1.8:/usr/local/lib/ruby/1.8/:/usr/local/lib/ruby/site_r
    uby/1.8/i686-linux/:/usr/local/lib/ruby/1.8/i686-linux/:/usr/lib/ruby/site_ruby/1.8:/usr/lib/ruby/site_ruby/1.8/i686-linux:/usr/lib/
    ruby/site_ruby:/usr/lib/ruby/1.8:/usr/lib/ruby/1.8/i686-linux:.
    /backups/scripts/delete_old.rb
    /usr/bin/ruby -I
    /usr/local/lib/ruby/site_ruby:/usr/local/lib/ruby/site_ruby/1.8:/usr/local/lib/ruby/1.8/:/usr/local/lib/ruby/site_r
    uby/1.8/i686-linux/:/usr/local/lib/ruby/1.8/i686-linux/:/usr/lib/ruby/site_ruby/1.8:/usr/lib/ruby/site_ruby/1.8/i686-linux:/usr/lib/
    ruby/site_ruby:/usr/lib/ruby/1.8:/usr/lib/ruby/1.8/i686-linux:.
    /backups/scripts/spn_delete_old.rb
    ---[/batch_delete.sh]---

    It's not an elegant solution but it works fine.. and noboby is going to
    snoop around my server files anyways.. :)

    cheers,

    -Andreas
     
    , Mar 21, 2005
    #17
  18. <> schrieb im Newsbeitrag
    news:...
    >
    > Robert Klemme wrote:
    > >
    > > You probably should set $LOAD_PATH explicitely in the script. Try
    > > ruby -e 'p $LOAD_PATH'
    > > and see what it prints from the command line and from cron.
    > >

    >
    > Shell: 375 Bytes
    > Cron: 159 Bytes
    >
    > I get a huge difference between executing this command on the shell and
    > as cronjob.. how comes?
    >
    > How do I set the $LOAD_PATH? I tried to set it inside my ruby script
    > but I summoned an error..


    $LOAD_PATH <<
    "/usr/lib/whatever" <<
    "/foo/bar"

    You can as well do

    $LOAD_PATH.clear <<
    "/usr/lib/whatever" <<
    "/foo/bar"

    if you want to remove all initial content. Or you use concat

    $LOAD_PATH.clear.concat [
    "/usr/lib/whatever",
    "/foo/bar",
    ]

    $: works as well (less typing but not as good from a documentation
    perspective). As you see there are zillions of ways to do it. You can
    even delete individual entries etc. $LOAD_PATH is just a normal array.
    :)

    Kind regards

    robert
     
    Robert Klemme, Mar 21, 2005
    #18
  19. <> schrieb im Newsbeitrag
    news:...
    >
    > Robert Klemme wrote:
    > > Try doing this on the shell and set $LOAD_PATH accordingly before the

    > first
    > > require:
    > >
    > > ruby -e 'puts $LOAD_PATH.join(":")'
    > >
    > > Alternatively you might have a permissions problem but I think path

    > is more
    > > likely.
    > >
    > > Kind regards
    > >
    > > robert

    >
    > Good Evening Robert!
    >
    > Thanks for supporting me with my silly but serious problem.. :)
    >
    > I have spotted a big difference between the $LOAD_PATH of the shell and
    > executing the same from the cronjob. How can I set the correct
    > $LOAD_PATH inside the rubyscript?
    >
    > $LOAD_PATH of the shell:
    >

    /usr/local/lib/ruby/site_ruby:/usr/local/lib/ruby/site_ruby/1.8:/usr/local
    /lib/ruby/1.8/:/usr/local/lib/ruby/site_ruby/1.8/i686-linux/:/usr/local/li
    b/ruby/1.8/i686-linux/:/usr/lib/ruby/site_ruby/1.8:/usr/lib/ruby/site_ruby
    /1.8/i686-linux:/usr/lib/ruby/site_ruby:/usr/lib/ruby/1.8:/usr/lib/ruby/1.
    8/i686-linux:.
    >
    > $LOAD_PATH of the cronjob:
    >

    /usr/lib/ruby/site_ruby/1.8:/usr/lib/ruby/site_ruby/1.8/i686-linux:/usr/li
    b/ruby/site_ruby:/usr/lib/ruby/1.8:/usr/lib/ruby/1.8/i686-linux:.
    >
    > Now that's a difference.. :)
    >
    > Any suggestions on how I may add the missing paths?


    See my posting to the other branch of the thread.

    robert
     
    Robert Klemme, Mar 21, 2005
    #19
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rajat
    Replies:
    3
    Views:
    705
    Jorgen Grahn
    Jan 8, 2010
  2. Sy
    Replies:
    0
    Views:
    189
  3. Mario Gr
    Replies:
    3
    Views:
    304
    Mario Gr
    Jul 4, 2009
  4. Saravanan Sundaramoorthy

    how to test a ruby script from another ruby script?

    Saravanan Sundaramoorthy, Jul 17, 2010, in forum: Ruby
    Replies:
    1
    Views:
    147
    Roger Pack
    Jul 19, 2010
  5. 3lionz Wexler
    Replies:
    2
    Views:
    676
    Suraj Kurapati
    Aug 24, 2010
Loading...

Share This Page