[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling Ruby from PHP fails on "requires 'mysql'"

Mark Meijer

9/29/2007 8:53:00 PM


I'm calling Ruby from PHP using system().
This works fine until I add "require 'mysql'".

I think it's likely missing a path for the Ruby MySQL gem.


I can call the same script successfully from crontab by adding the line
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))

Any experience or ideas?

cheers
--
Posted via http://www.ruby-....

8 Answers

Jano Svitok

9/29/2007 10:00:00 PM

0

On 9/29/07, Mark Meijer <mtlmeijer@gmail.com> wrote:>> I'm calling Ruby from PHP using system().> This works fine until I add "require 'mysql'".>> I think it's likely missing a path for the Ruby MySQL gem.>>> I can call the same script successfully from crontab by adding the line> $LOAD_PATH << File.expand_path(File.dirname(__FILE__))>> Any experience or ideas?If the problem is in gems, try adding require "rubygems" before require "mysql".(the same may be achieved by setting RUBYOPT=rubygems in the environment)If it doesn't help, please post the exact error message with exception trace.

Mark Meijer

9/29/2007 10:27:00 PM

0


Thanks Jano,


I am already including rubygems, here's my requires list..

$LOAD_PATH << File.expand_path(File.dirname(__FILE__))
require 'rubygems'
require 'mysql'

I'm not seeing an error message when I call my Ruby script from PHP with

$s = sprintf("ruby /Users/mark/scripts/myscript.rb >
/Users/mark/scripts/myscript.log &");
system($s);

I see the puts statements from my script in the log file, no errors.

--
Posted via http://www.ruby-....

Mark Meijer

9/29/2007 11:34:00 PM

0



Execution of my script ends at "require 'mysql'".

puts statements before it are printed to the log file, then nothing.

How can I see the error there?


thanks again

--
Posted via http://www.ruby-....

Jano Svitok

9/30/2007 7:01:00 PM

0

On 9/30/07, Mark Meijer <mtlmeijer@gmail.com> wrote:
>
>
> Execution of my script ends at "require 'mysql'".
>
> puts statements before it are printed to the log file, then nothing.
>
> How can I see the error there?

1. try adding -w switch to ruby to see the warnings (ruby -w ...)
2. try saving stderr as well : (... 2>/Users/.../myscript.err)
3. I'm not sure what will that & do -- especially what will happen if
the calling php script stops.

Jano

Mark Meijer

10/1/2007 7:35:00 PM

0





Thanks again!


Saving the stderr gave me

/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
`gem_original_require': No such file to load -- mysql (LoadError)
from
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
from /Users/mark/scripts/myscript.rb:31

Line 31 is
require 'mysql'


So, looks like it can't find the gem in the path.

I'm able to call the same script from crontab by adding
$LOAD_PATH << File.expand_path(File.dirname(__FILE__))


Any ideas on massaging the path?

cheers

--
Posted via http://www.ruby-....

Jano Svitok

10/1/2007 7:54:00 PM

0

On 10/1/07, Mark Meijer <mtlmeijer@gmail.com> wrote:
>
>
>
>
> Thanks again!
>
>
> Saving the stderr gave me
>
> /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in
> `gem_original_require': No such file to load -- mysql (LoadError)
> from
> /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
> from /Users/mark/scripts/myscript.rb:31
>
> Line 31 is
> require 'mysql'
>
>
> So, looks like it can't find the gem in the path.
>
> I'm able to call the same script from crontab by adding
> $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
>
>
> Any ideas on massaging the path?
>
> cheers

What came to my mind is whether are you using the same ruby in those two cases?

Other than that, 1. look up where the mysql lib/gem resides, 2. print
out $LOAD_PATH in both cases and compare.

You could do system("gem list") as well to check whether gems can find
the mysql gem.

Mark Meijer

10/1/2007 10:17:00 PM

0


Ok, getting close.


The Ruby version called from PHP is indeed different from the Ruby
called from command line or crontab..

from PHP
/usr/bin/ruby
ruby 1.8.2 (2004-12-25) [universal-darwin8.0]

otherwise
/usr/local/bin/ruby
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.8.2]


I tried adding the path to a system call but it doesn't help
the ruby call and I don't get anything written to the logs..

$s = sprintf("echo $PATH > /Users/../path0.log 2> /Users/../path1.log");
system($s);
$s = sprintf("export PATH=/usr/local/bin:/usr/local/mysql/bin:$PATH");
system($s);
$s = sprintf("echo $PATH > /Users/../path2.log 2> /Users/../path3.log");
system($s);




--
Posted via http://www.ruby-....

John Joyce

10/1/2007 11:32:00 PM

0


On Oct 1, 2007, at 5:17 PM, Mark Meijer wrote:

>
> Ok, getting close.
>
>
> The Ruby version called from PHP is indeed different from the Ruby
> called from command line or crontab..
>
> from PHP
> /usr/bin/ruby
> ruby 1.8.2 (2004-12-25) [universal-darwin8.0]
>
> otherwise
> /usr/local/bin/ruby
> ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.8.2]
>
>
> I tried adding the path to a system call but it doesn't help
> the ruby call and I don't get anything written to the logs..
>
> $s = sprintf("echo $PATH > /Users/../path0.log 2> /Users/../
> path1.log");
> system($s);
> $s = sprintf("export PATH=/usr/local/bin:/usr/local/mysql/bin:$PATH");
> system($s);
> $s = sprintf("echo $PATH > /Users/../path2.log 2> /Users/../
> path3.log");
> system($s);
>
>
>
>
> --
> Posted via http://www.ruby-....
>
*nix trouble!

PHP probably doesn't run as your user account, so it might use a
different PATH. The path you're using for Ruby is for YOUR user's
shell login.
one way to verify it is to check the user id and process id. you can
use tail for that.
PHP probably uses something set in /env
You would have the same path to Ruby if you didn't have the path
variable set by the dot file in your user account directory when you
do a shell login.
PHP often runs as whatever Apache runs as for user id, and it's not
your user id.
The PHP script may be owned by you, but the process is owned by PHP
or Apache.
The PHP CLI works differently. It runs under your uid, just as Ruby
would.
Anything run by Apache or another server will have a different uid.