Call ruby script from another user not using rvm - ruby

When I need to run a script from my linux user "user1", I do the following commands (right after login with "user1"):
rvm use 1.9.3
cd /var/proj
ruby main.rb
When developing this script, I have created a bundle with bundle install, reading my Gemfile (containing httparty and other stuff).
This is working fine (no reason it would not work :) )
When I log with another user "user2", I'd like to execute the script but cannot have it working:
cd /var/proj
/home/user1/.rvm/rubies/ruby-1.9.3-p327/bin/ruby main.rb
The error I get:
/home/user1/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- httparty (LoadError)
from /home/user1/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from main.rb:3:in `<main>'
It seems it cannot see the bundle created by the other user. Any idea of what needs to be changed ?
I'd like to run this script within supervisor but this is the same thing.

It is not the recommended but you can use RVM from other a/c . Just set the rvm required path in another user a/c.
Details instruction:
First go to user1 and follow this steps
rvm use 1.9.3
echo $PATH
now copy the path which include rvm word , in my case it is
/home/paritosh/.rvm/gems/ruby-1.9.3-p194/bin:/home/paritosh/.rvm/gems/ruby-1.9.3-p194#global/bin:/home/paritosh/.rvm/rubies/ruby-1.9.3-p194/bin:/home/paritosh/.rvm/bin
now go to another user2 a/c and use PATH=copied_path:$PATH so in my case it is
PATH=/home/paritosh/.rvm/gems/ruby-1.9.3-p194/bin:/home/paritosh/.rvm/gems/ruby-1.9.3-p194#global/bin:/home/paritosh/.rvm/rubies/ruby-1.9.3-p194/bin:/home/paritosh/.rvm/bin:$PATH
Now go to
cd /var/proj
ruby main.rb
btw, if you want to make the path permanent put the PATH in ~/.bashrc of user2 a/c .

Related

create Scheduled Task from working Ruby script on Ubuntu inside Windows10

I have a Windows10 Ubuntu bash environment set up. It has a few rubygems installed (without docker, bundler, without rvm, and also this is not rails). The script is working fine when I run it manually from inside the Ubuntu terminal, in the user home folder ~/
Now my goal is create a Windows Scheduled Task using the Task Scheduler application, to run the script daily, like I would with a crontab in Unix.
The action in this Scheduled Task is the challenge. In testing manually from the Windows CMD prompt, I got as far as:
C:\>C:\Windows\System32\bash.exe -c "ruby ~/myscript.rb"
That fails :
Traceback (most recent call last):
2: from /home/lam/ruby/remind_prepare_dad_before_iterative_ends.rb:15:in `<main>'
1: from /usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/usr/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- tiny_tds (LoadError)
Above error tells me it cannot find my gem, TinyTDS. What do I add to the command line to make it find my gems?
I found a crude workaround for my own problem: just install the gem from the same command line:
C:\>C:\Windows\System32\bash.exe -c "sudo gem install tiny_tds"
I had to re-install a few more gems I use in the same way.
After this, I put in a -C argument to start ruby in the right directory, like so:
C:\>C:\Windows\System32\bash.exe -c "ruby -C ~/ruby myscript.rb"
works like a charm.
I suppose now I have duplicated gems floating in spaces on the machine, but we will address that issue if it ever becomes a problem. :-)
Try bundle exec ruby ~/myscript.rb

how to add rvm to super user

hi i need to execute a ruby program from a init script i need to run the script as
sudo /etc/bin/differ.sh start
but the problem is rvm is not installed on root(super user) . so code in the differ script ruby "filename" wont execute because the gems and rvm is not installed in root.
i need solve how to add rvm and its gems to root (i have not installed by system wide installation).i have even tried the rvmsudo
rvmsudo ruby "filename"
but it's not working in script works good in terminal.
please free to ask any files or outputs i will post along with this question.
This page on integrating RVM with init.d may help: https://rvm.io/integration/init-d

Superuser Issue in ruby kubuntu

I have installed ruby and gems with sudo,
Whenever I run the script, I have to do it with sudo or else I will get this error:
/usr/local/rvm/rubies/ruby-1.9.3-p385/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- watir-webdriver (LoadError)
from /usr/local/rvm/rubies/ruby-1.9.3-p385/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'from Login.rb:1:in `<main>'
I am planning to use eclipse IDE, so--if I do run the script in eclipse--I get the same problem
Could someone help me in running ruby script as superuser in eclipse?
You can change the access permissions for needed gem.
sudo chmod 644 filename
To know the full path:
gem which gemname

Problems with heroku toolbelt

I'm absolute newbie in Ruby and Rails project, so I'm sorry if this is dumb question.
I've installed heroku toolbelt using wget -qO- https://toolbelt.heroku.com/install-ubuntu.sh | sh. Also I tried gem install heroku. But when I type:
MY_USER#home-PC:~$ heroku version
<internal:gem_prelude>:1:in `require': cannot load such file -- rubygems.rb (LoadError)
from <internal:gem_prelude>:1:in `<compiled>'
I've checked in google and people suggest that gem and current ruby are different versions so I've checked it and they seem to the same:
MY_USER#home-PC:~$ which ruby
/home/MY_USER/.rvm/rubies/ruby-1.9.3-p327/bin/ruby
MY_USER#home-PC:~$ gem env | grep 'RUBY EXECUTABLE'
- RUBY EXECUTABLE: /home/MY_USER/.rvm/rubies/ruby-1.9.3-p327/bin/ruby
I have no idea what to do more, so please help.
Sorry for the maybe stupid question, thanks in advance.
EDIT:
Forgot to say I'm running Ubuntu 12.10.
The problem is that the heroku executable you installed probably starts with a line like this:
#!/usr/bin/ruby
This will force the heroku command to always use the system-wide ruby (/usr/bin/ruby) and it will never run your rvm version of ruby.
To fix it simply edit the first line of the heroku script to this:
#!/usr/bin/env ruby
This will make the heroku command run whichever ruby command is in the current PATH, instead of a hard coded path like previously.
To find the location of the heroku script, so you can edit it, simply type:
which heroku
It should print out the location of the script so you can find it and load it into your editor.
Also if you do not have write permission you may enter:
:w !sudo tee % > /dev/null
to save successfully .

rake install does not work for octopress

I am trying to setup rake via this doc.
http://octopress.org/docs/setup/
But I get some errors.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ rake install
## Copying classic theme into ./source and ./sass
mkdir -p source
rake aborted!
Permission denied - source
Tasks: TOP => install
(See full trace by running task with --trace)
With sudo I get this output.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ sudo rake install
rake aborted!
no such file to load -- bundler/setup
(See full trace by running task with --trace)
Here is a list of files in the directory.
ikhthiandor#ikhthiandor-Satellite-L450:/opt/octopress$ ls -a
. config.ru .git Rakefile .slugignore
.. _config.yml .gitignore .rbenv-version .themes
CHANGELOG.markdown Gemfile plugins README.markdown
config.rb Gemfile.lock .powrc .rvmrc
How can I solve the problem?
Ikhthiandor: Looks like you are a beginner in the ruby/rails world.
By running the rake install command, you are installing the default octopress theme using the rake tool. Not trying to setup rake as you mention in the question.
The first error ( Permission denied - source when trying mkdir -p source - as you correctly guessed - is because the user doesn't have permissions to create that directory.
The second error ( no such file to load -- bundler/setup ) is because the previous install dependencies steps are not performed correctly (for the user running this command).
The install dependencies steps to be completed successfully are:
1. gem install bundler
2. rbenv rehash # If you use rbenv, rehash to be able to run the bundle command
3. bundle install
I am guessing you ran these steps successfully as 'ikhthiandor' user, so the bundler gem is not available for the 'sudo' user.
You can fix this by either of the following options:
changing permissions on /opt/octopress folder so that 'ikhthiandor' user has rights to create sub-folders/files within.
Run all the commands in the Octopress Setup doc as 'sudo'
Best practice would be to use either rvm or rbenv to manage custom installations of ruby environments per user (and not do everything as super user).
If you are indeed a fresher in the ruby-rails world, and want to step up your knowledge of the tools and the best practices in the ruby/rails world, I suggest browsing through the first few chapters of the Ruby on Rails Tutorial book that is available for free on-line.
HTH

Resources