Crobjob problem with ruby script.

A

andreas.cahen

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
 
J

James Britt

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
 
R

Robert Klemme

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

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
 
J

James Britt

Robert said:
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
 
A

andreas.cahen

Robert said:
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
 
S

Shad Sterling

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
 
A

Aredridel

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.
 
S

Shad Sterling

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
 
J

Jostein Berntsen

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
 
A

andreas.cahen

Hi..

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

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

-Andreas
 
P

Premshree Pillai

Hi..

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

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

-Andreas

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

Mark Probert

Hi ..

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,
 
A

andreas.cahen

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
 
R

Robert Klemme

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
 
A

andreas.cahen

Robert said:
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
 
A

andreas.cahen

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
 
R

Robert Klemme

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
 
R

Robert Klemme

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top