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

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

On 9/29/07, Mark M. [email protected] wrote:

Any experience or ideas?
If the problem is in gems, try adding require “rubygems” before require
“mysql”.
(the same may be achieved by setting RUBYOP«=rubygems in the
environment)

If it doesn’t help, please post the exact error message with exception
trace.

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.

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

On 9/30/07, Mark M. [email protected] 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

On 10/1/07, Mark M. [email protected] wrote:

`gem_original_require’: No such file to load – mysql (LoadError)
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.

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:inrequire’
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

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);

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

system($s);
Posted via http://www.ruby-forum.com/.

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