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?
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?
crontab wants the full path to the ruby executable.
- Jostein
···
On 18.03.05,23:24, andreas.cahen@gmail.com 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?
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
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..
<andreas.cahen@gmail.com> schrieb im Newsbeitrag news:1111334766.888543.173130@l41g2000cwc.googlegroups.com...
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.
<andreas.cahen@gmail.com> schrieb im Newsbeitrag
news:1111161279.684304.8420@f14g2000cwb.googlegroups.com...
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.
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?
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
···
On Sat, 19 Mar 2005 02:59:52 +0900, andreas.cahen@gmail.com <andreas.cahen@gmail.com> 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
--
----------
Please do not send personal (non-list-related) mail to this address.
Personal mail should be sent to polyergic@sterfish.com.
<andreas.cahen@gmail.com> schrieb im Newsbeitrag
news:1111168534.355181.69980@g14g2000cwa.googlegroups.com...
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..
$: 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.
<andreas.cahen@gmail.com> schrieb im Newsbeitrag
news:1111353483.371443.9470@g14g2000cwa.googlegroups.com...
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?
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?)
> 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
···
On Sat, 19 Mar 2005 03:51:04 +0900, Aredridel <aredridel@gmail.com> wrote:
> (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 polyergic@sterfish.com.