error while running ruby application at system startup in ubuntu - ruby-on-rails

I am on Ubuntu 12.04 machine. Have a script file which runs when entered manually in terminal
gnome-terminal -e /home/precise/Desktop/cartodb/script.sh
The content of script file is
cd /home/ubuntupc/Desktop/cartodb20/
sh /home/ubuntupc/.rvm/scripts/rvm
bundle exec foreman start -p 3000
So what i tried to do is to run this script at every system start up. So on Startup Applications
command: gnome-terminal -e /home/precise/Desktop/cartodb/script.sh
On terminal Edit -> Profile Preferences -> Title and Command
Checked the "Run command as a login shell"
But this seems to be not working. When restarted the machine found these error in terminal
The child process exited normally with status 127.
ERROR: RVM Ruby not used, run `rvm use ruby` first.
Some info regarding the installed packages and system.
$ which ruby
/home/ubuntupc/.rvm/rubies/ruby-1.9.2-p320/bin/ruby
$ which rails
/home/ubuntupc/.rvm/gems/ruby-1.9.2-p320/bin/rails
$ which gem
/home/ubuntupc/.rvm/rubies/ruby-1.9.2-p320/bin/gem
$ cat ~/.bash_profile
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
$ which -a ruby
/home/ubuntupc/.rvm/rubies/ruby-1.9.2-p320/bin/ruby
$ sudo update-alternatives --config ruby
update-alternatives: error: no alternatives for ruby.
$ sudo find / -name "rubygems" -print
/home/ubuntupc/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/site_ruby/1.9.1/rubygems
/home/ubuntupc/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/rubygems
/home/ubuntupc/.rvm/src/ruby-1.9.2-p320/lib/rubygems
/home/ubuntupc/.rvm/src/ruby-1.9.2-p320/test/rubygems
/home/ubuntupc/.rvm/src/ruby-1.9.2-p320/test/rubygems/rubygems
/home/ubuntupc/.rvm/src/ruby-1.9.2-p320/doc/rubygems
/home/ubuntupc/.rvm/src/rubygems-2.2.1/lib/rubygems
/home/ubuntupc/.rvm/src/rubygems-2.2.1/test/rubygems
/home/ubuntupc/.rvm/src/rubygems-2.2.1/test/rubygems/rubygems
/home/ubuntupc/.rvm/src/rvm/scripts/functions/rubygems
/home/ubuntupc/.rvm/src/rvm/scripts/rubygems
/home/ubuntupc/.rvm/scripts/functions/rubygems
/home/ubuntupc/.rvm/scripts/rubygems
/usr/lib/ruby/1.9.1/rubygems
/usr/local/rvm/rubies/ruby-1.9.2-p320/lib/ruby/site_ruby/1.9.1/rubygems
/usr/local/rvm/rubies/ruby-1.9.2-p320/lib/ruby/1.9.1/rubygems
/usr/local/rvm/src/ruby-1.9.2-p320/lib/rubygems
/usr/local/rvm/src/ruby-1.9.2-p320/test/rubygems
/usr/local/rvm/src/ruby-1.9.2-p320/test/rubygems/rubygems
/usr/local/rvm/src/ruby-1.9.2-p320/doc/rubygems
/usr/local/rvm/src/rubygems-2.2.0/lib/rubygems
/usr/local/rvm/src/rubygems-2.2.0/test/rubygems
/usr/local/rvm/src/rubygems-2.2.0/test/rubygems/rubygems
/usr/local/rvm/src/rvm/scripts/functions/rubygems
/usr/local/rvm/src/rvm/scripts/rubygems
/usr/local/rvm/scripts/functions/rubygems
/usr/local/rvm/scripts/rubygems
Please point out what i am missing as i am new to the ruby applications.
Thanks in advance

Related

Why would only using rbenv and ruby-build work to install ruby on ubuntu?

I tried all the normal ways to install ruby: https://superuser.com/questions/340490/how-to-install-and-use-different-versions-of-ruby/1756372#1756372 in my cluster where I don't have admin permission (but they told me to install it locally myself).
The only solution that worked was:
#!/usr/bin/env bash
# -- this really is the only solution that worked for me on snap :/
ruby -v
if ! command -v ruby &> /dev/null
then
echo "Going to try to install ruby (ideally 3.1.2)"
# - install rbenv (following ruby-build really is needed eventhough it doesn't look like it)
mkdir -p ~/.rbenv
cd ~/.rbenv
git clone https://github.com/rbenv/rbenv.git .
# if $HOME/.rbenv/bin not in path append it, otherwise don't change it
echo $PATH | tr ':' '\n' | awk '{print " " $0}';
if [[ ":$PATH:" != *":$HOME/.rbenv/bin:"* ]]; then
echo "might want to put $HOME/.rbenv/bin in your path"
export PATH="$HOME/.rbenv/bin:$PATH"
# echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc.lfs
fi
eval "$(rbenv init -)"
rbenv -v
# - install ruby-build, odd, this really is needed for ruby to install despite it not looking like ruby build is need at the bottom
mkdir -p ~/.ruby-build
cd ~/.ruby-build
git clone https://github.com/rbenv/ruby-build.git .
# if $HOME/.ruby-build/bin not in path append it, otherwise don't change it
echo $PATH | tr ':' '\n' | awk '{print " " $0}';
if [[ $PATH != *"$HOME/.ruby-build/bin"* ]]; then
echo "might want to put $HOME/.ruby-build/bin in your path"
export PATH="$HOME/.ruby-build/bin:$PATH"
# echo 'export PATH="$HOME/.ruby-build/bin:$PATH"' >> ~/.bashrc.lfs
fi
ruby-build --version
# - install ruby without sudo -- using rbenv
mkdir -p ~/.local
# ruby-build 3.1.2 ~/.local/
rbenv install 3.1.2
rbenv global 3.1.2
fi
ruby -v
# - Original Prover doesn't work on SNAP
# Proverbot's way to install ruby
# # First, install Ruby, as that is for some reason required to build the "system" project
# git clone https://github.com/rbenv/ruby-build.git ~/ruby-build
# mkdir -p ~/.local
# PREFIX=~/.local ./ruby-build/install.sh
# ~/.local/ruby-build 3.1.2 ~/.local/
# ref: https://superuser.com/questions/340490/how-to-install-and-use-different-versions-of-ruby/1756372#1756372
why? Is there something simpler or more official? (I do need 3.1.2)
related:
How do I install Ruby 1.9.3 on Ubuntu without RVM?
Unable to install Ruby on Ubuntu
The script looks fine to me, as you've no admin rights, the only choice that you have is to install it in the home of the user that needs it, and rbenv is the right tool to handle that.
The script is just aggregating all the required commands to install everything you need:
rbenv (ruby environments manager) => https://github.com/rbenv/rbenv#basic-git-checkout
ruby-build (ruby versions manager) => https://github.com/rbenv/ruby-build#clone-as-rbenv-plugin-using-git
ruby 3.1.2 (ruby version that you want) => https://github.com/rbenv/rbenv#installing-ruby-versions

Configure unicorn service script using RVM

In my /etc/init.d/unicorn startup script I have the hard-coded paths as follows:
export GEM_HOME=/usr/local/rvm/gems/ruby-2.2.0-dev
export GEM_PATH=/usr/local/rvm/gems/ruby-2.2.0-dev:/usr/local/rvm/gems/ruby-2.2.0-dev/gems:/usr/local/rvm/gems/ruby-2.2.0-dev#global/gems
DAEMON=/usr/local/rvm/gems/ruby-2.2.0-dev/bin/unicorn
UNICORN_OPTS="-D -c /home/unicorn/unicorn.conf -E production"
I am using RVM and when I change the ruby version, then the current ruby should be used for unicorn.
Question 1
So how can I make sure that these variables always point to the proper ruby?
Question 2
In my bundle I am using rack 1.5.5. Now on my production server I had to install the unicorn gem as a "stand-alone-gem" so that I can start my server using:
service unicorn start
Now the unicorn gem installed rack 1.6.x and now my Rails app crashed because rack is already loaded. Now locally I would just execute it with bundle but how can I do that when I am using this /etc/inid.d script.
The part where the server is started looks like follows, and I don't know how I could "inject" the bundler call there or if that is a good practise at all:
# ...
log_daemon_msg "Starting $DESC" $NAME || true
if start-stop-daemon --start --quiet --oknodo --pidfile $PID --exec $DAEMON -- $UNICORN_OPTS; then
The config: /home/unicorn/unicorn.conf
listen "127.0.0.1:8080"
worker_processes 2
user "rails"
working_directory "/home/myapp"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"
Assuming rubyuser has all these rvm and rubies installed, one might parse the output of rvm info, running the latter from this user:
`sudo -H -u rubyuser bash -c '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" && rvm info | grep "^ \+GEM_"| sed -e "s/^ \+/export /" -e "s/: \+/=/"'`
The command above will set the variables GEM_HOME and GEM_PATH. To just output them, remove backticks around this command.
For rvm installed in local, it’s even simpler:
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" && rvm info | grep "^ \+GEM_"| sed -e "s/^ \+/export /" -e "s/: \+/=/"
# export GEM_HOME="/usr/local/rvm/gems/ruby-2.1.1"
# export GEM_PATH="/usr/local/rvm/gems/ruby-2.1.1:/usr/local/rvm/gems/ruby-2.1.1#global"

How to always run php in console interactively way not to exit?

I am in debian7.8+php5.3 .
root#debian:/home/debian# php -a
Interactive mode enabled
<?php
echo "hello";
?>
No reaction when to click enter ,to click ctrl+D can get the output:
hello
But it will exit from php Interactive mode into debian console.
root#debian:/home/debian#
How to always run php in console interactively way not to exit ?
You can try use phpsh - An interactive shell for php
How to install phpsh
$ sudo apt-get install python # this is necessary to run phpsh
$ cd ~/
$ wget https://github.com/facebook/phpsh/zipball/master
$ unzip phpsh-master.zip
$ cd phpsh-master
$ sudo cp -r src /etc/phpsh # phpsh seems to complain unless it resides at /etc/phpsh
$ sudo ln -s /etc/phpsh/phpsh /usr/bin/phpsh # put phpsh on the $PATH
Run phpsh
$ phpsh
Starting php
type 'h' or 'help' to see instructions & features
php> echo 'hello world';
hello world
php>

Start Unicorn with Runit and User's RVM

I'm deploying my Rails App servers with Chef. Have just swapped to RVM from a source install of Ruby (because I was having issues with my deploy user).
Now I have my deploy sorted, assets compiled and bundler's installed all my gems.
The problem I have is supervising Unicorn with Runit..
RVM is not installed as root user - only as my deploy user has it, as follows:
$ rvm list
rvm rubies
=* ruby-2.0.0-p247 [ x86_64 ]
I can manually start Unicorn successfully from my deploy user. However, it won't start as part of runit.
My run file looks like this. I have also tried the solution in this SO question unsuccessfully..
#!/bin/bash
cd /var/www/html/deploy/production/current
exec 2>&1
exec chpst -u deploy:deploy /home/deploy/.rvm/gems/ruby-2.0.0-p247/bin/unicorn -E production -c config/unicorn_production.rb
If I run it manually, I get this error:
/usr/bin/env: ruby_noexec_wrapper: No such file or directory
I created a small script (gist here) which does run as root. However, if I call this from runit, I see the workers start but I get two processes for runit and I can't stop or restart the service:
Output of ps:
1001 29062 1 0 00:08 ? 00:00:00 unicorn master -D -E production -c /var/www/html/deploy/production/current/config/unicorn_production.rb
1001 29065 29062 9 00:08 ? 00:00:12 unicorn worker[0] -D -E production -c /var/www/html/deploy/production/current/config/unicorn_production.rb
root 29076 920 0 00:08 ? 00:00:00 su - deploy -c cd /var/www/html/deploy/production/current; export GEM_HOME=/home/deploy/.rvm/gems/ruby-2.0.0-p247; /home/deploy/.rvm/gems/ruby-2.0.0-p247/bin/unicorn -D -E production -c /var/www/html/deploy/production/current/config/unicorn_production.rb
1001 29083 29076 0 00:08 ? 00:00:00 -su -c cd /var/www/html/deploy/production/current; export GEM_HOME=/home/deploy/.rvm/gems/ruby-2.0.0-p247; /home/deploy/.rvm/gems/ruby-2.0.0-p247/bin/unicorn -D -E production -c /var/www/html/deploy/production/current/config/unicorn_production.rb
What should I do here? Move back to monit which worked nicely?
your run file is doing it wrong, you are using the binary without setting the environment, for that purpose you should use wrappers:
rvm wrapper ruby-2.0.0-p247 --no-links unicorn
To simplify the script use alias so it does not need to be changed when you decide which ruby should be used:
rvm alias create my_app_unicorn ruby-2.0.0-p247
And change the script to:
#!/bin/bash
cd /var/www/html/deploy/production/current
exec 2>&1
exec chpst -u deploy:deploy /home/deploy/.rvm/wrappers/my_app_unicorn/unicorn -E production -c config/unicorn_production.rb
This will ensure proper environment is used for execution of unicorn and any time you want change ruby used to run it just crate alias to a new ruby.

Manage sidekiq with init.d script using RVM

I'm using the init.d script provided (the init.d script from the sidekiq github repo), but I am on an ubuntu system with RVM installed system wide.
I cannot seem to figure out how to cd into my app directory and issue the command without there being some complaining in the log and nothing actually starting.
Question: What should the startup command for sidekiq look like in my init.d script when I am using RVM? My user is named ubuntu. Currently I have this in my init.d script:
START_CMD="$BUNDLE exec $SIDEKIQ"
# where bundle is /usr/local/rvm/gems/ruby-1.9.3-p385/bin/bundle
# and sidekiq is sidekiq
# I've also tried with the following args: -e $APP_ENV -P $PID_FILE -C $APP_CONFIG/sidekiq.yml -d -L $LOG_FILE"
RETVAL=0
start() {
status
if [ $? -eq 1 ]; then
[ `id -u` == '0' ] || (echo "$SIDEKIQ runs as root only .."; exit 5)
[ -d $APP_DIR ] || (echo "$APP_DIR not found!.. Exiting"; exit 6)
cd $APP_DIR
echo "Starting $SIDEKIQ message processor .. "
echo "in dir `pwd`"
su - ubuntu -c "$START_CMD >> $LOG_FILE 2>&1 &"
RETVAL=$?
#Sleeping for 8 seconds for process to be precisely visible in process table - See status ()
sleep 8
[ $RETVAL -eq 0 ] && touch $LOCK_FILE
return $RETVAL
else
echo "$SIDEKIQ message processor is already running .. "
fi
}
My sidekiq.log gives me this error:Could not locate Gemfile. However, I print the working directory and I am most definitely in my app's current directory, according to the echo pwd, at the time this command is executed.
When I take out the su - ubuntu -c [command here], I get this error:
/usr/bin/env: ruby_noexec_wrapper: No such file or directory
My solution is to just start the process manually. When I manually cd into my app directory and issue this command:
bundle exec sidekiq -d -L log/sidekiq.log -P tmp/pids/sidekiq.pid
things go as planned, and then
sudo /etc/init.d/sidekiq status
tells me things are up and running.
Also, sudo /etc/init.d/sidekiq stop and status work as expected.
I wrote a blog post a few months ago on my experience writing an init.d script for Sidekiq, however I was using rbenv rather than RVM.
https://cdyer.co.uk/blog/init-script-for-sidekiq-with-rbenv/
I think you should be able to use something almost identical except for modifying the username and app dir variables.
use wrappers:
BUNDLER=/usr/local/rvm/wrappers/ruby-1.9.3-p385/bundle
in case the bundler wrapper is not available generate it with:
rvm wrapper ruby-1.9.3-p385 --no-links bundle # OR:
rvm wrapper ruby-1.9.3-p385 --no-links --all
you can use aliases to make it easier:
rvm alias create my_app 1.9.3-p385
and then use it like this:
BUNDLER=/usr/local/rvm/wrappers/my_app/bundle
this way you will not have to change the script when application ruby changes - just update the alias, there is a good description/integration for this in rvm-capistrano => https://github.com/wayneeseguin/rvm-capistrano/#create-application-alias-and-wrappers

Resources