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

M

Mark Meijer

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
 
J

Jano Svitok

T24gOS8yOS8wNywgTWFyayBNZWlqZXIgPG10bG1laWplckBnbWFpbC5jb20+IHdyb3RlOgo+Cj4g
SSdtIGNhbGxpbmcgUnVieSBmcm9tIFBIUCB1c2luZyBzeXN0ZW0oKS4KPiBUaGlzIHdvcmtzIGZp
bmUgdW50aWwgSSBhZGQgInJlcXVpcmUgJ215c3FsJyIuCj4KPiBJIHRoaW5rIGl0J3MgbGlrZWx5
IG1pc3NpbmcgYSBwYXRoIGZvciB0aGUgUnVieSBNeVNRTCBnZW0uCj4KPgo+IEkgY2FuIGNhbGwg
dGhlIHNhbWUgc2NyaXB0IHN1Y2Nlc3NmdWxseSBmcm9tIGNyb250YWIgYnkgYWRkaW5nIHRoZSBs
aW5lCj4gJExPQURfUEFUSCA8PCBGaWxlLmV4cGFuZF9wYXRoKEZpbGUuZGlybmFtZShfX0ZJTEVf
XykpCj4KPiBBbnkgZXhwZXJpZW5jZSBvciBpZGVhcz8KCklmIHRoZSBwcm9ibGVtIGlzIGluIGdl
bXMsIHRyeSBhZGRpbmcgcmVxdWlyZSAicnVieWdlbXMiIGJlZm9yZSByZXF1aXJlICJteXNxbCIu
Cih0aGUgc2FtZSBtYXkgYmUgYWNoaWV2ZWQgYnkgc2V0dGluZyBSVUJZT1CrPXJ1YnlnZW1zIGlu
IHRoZSBlbnZpcm9ubWVudCkKCklmIGl0IGRvZXNuJ3QgaGVscCwgcGxlYXNlIHBvc3QgdGhlIGV4
YWN0IGVycm9yIG1lc3NhZ2Ugd2l0aCBleGNlcHRpb24gdHJhY2UuCg==
 
M

Mark Meijer

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

Mark Meijer

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
 
J

Jano Svitok

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
 
M

Mark Meijer

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
 
J

Jano Svitok

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

Mark Meijer

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

John Joyce

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

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
473,979
Messages
2,570,185
Members
46,728
Latest member
FernMcmull

Latest Threads

Top