I'm working with ec2 instances and was trying to execute a ruby script on another instance after ssh to that instance. I have a ruby script which updates configuration files, so i need to run that script as super user. when i run the script manually on that instance, sudo ruby recreate-532d01c.rb, the error that comes is

sudo: ruby: command not found

Running simple scripts with no root permissions works, eg.ruby file_1.rb.

Using rvmsudo in place of sudo executes the script with warning,

ubuntu@ip-10-0-0-111:~$ rvmsudo ruby recreate-82bb000012.rb 
Warning: can not check `/etc/sudoers` for `secure_path`, falling back to call via `/usr/bin/env`, this breaks rules from `/etc/sudoers`. Run:

    export rvmsudo_secure_path=1

to avoid the warning, put it in shell initialization file to make it persistent.

In case there is no `secure_path` in `/etc/sudoers`. Run:

    export rvmsudo_secure_path=0

to avoid the warning, put it in shell initialization file to make it persistent.

I tried to execute the below command from rails console of one of the instance to test and it fails to recognize ruby as command

1.9.3-p545 :002 > system("ssh -i /home/ubuntu/.ssh/own_key.pem ubuntu@**.***.***.** ruby execute-52d.rb")
bash: ruby: command not found

I tried with possible solutions over web, but could not resolve the issue. I have the same configuration running for one of my old aws acount, this is a newly created account. Not sure if this could be issue in any way as currently ec2 instances fall under vpc by default and have some changes after dec 2013

  • 1
    Sounds like a simple PATH issue. Just run type ruby and then with sudo use the full path to the ruby executable.
    – terdon
    Apr 14 '14 at 15:17
  • i will check with this. thanx
    – Bijendra
    Apr 14 '14 at 15:59
  • @terdon i have installed rvm as a single user in the instance in which i have scripts to run after ssh. Do i need to install multi user rvm or it will work
    – Bijendra
    Apr 14 '14 at 17:09
  • I don't know, I don't use ruby but the error you get is not ruby-specific. It is simply because the ruby executable in not in root's path. Does it work if you use the whole path as I suggested?
    – terdon
    Apr 14 '14 at 17:10
  • Thanx it worked
    – Bijendra
    Apr 14 '14 at 20:53

The error you are getting indicates that ruby is not in the sudo $PATH. TO get around this, you either need to export your environment correctly:

sudo -E ruby

Or, just use the full path. Run type ruby as your regular user, and use that with sudo. For example:

$ type ruby
ruby is /usr/bin/ruby
$ sudo /usr/bin/ruby

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.