Execute script with Ruby on Rails? - ruby-on-rails

I want to start my daemon with my application.
In the command line, I can write something like lib/daemons/mydaemon_ctl start to start up my daemon, but I have to do this manually. I want the daemon to start when I start my server (i.e. when the initializer files are loaded).
Is there a ruby command for executing a command line?
Something like exec "lib/daemons/mydaemon_ctl start"?
Thanks!

Seems you just want to run shell commands in ruby code, well you can use system or backtick(`)
system 'ls' # will return ls output in *nix
`dir` # will return dir output in windows

Related

How to run commands on ruby-on-rails console inside bash shell script?

I would like to know how to run ruby commands in ruby on rails console inside a shell script ?
To be clear: I have a shell script which will connect to ruby on rails console, but unable to run commands inside the rails console from shell script. Please see below code snippet.
I am successfully able to enter into rails console from shell script, but unable to provide ruby commands inside shell script itself.
This sample script is allowing me to enter into rails console:
#!/bin/bash
/usr/bin/helpkit-docker.sh --rails-console
But, when i extend the above script to run commands inside rails console using EOF, it throws error:
#!/bin/bash
/usr/bin/helpkit-docker.sh --rails-console <<EOF
Time.now.strftime("%d/%m/%Y %H:%M")
EOF
Error: the input device is not a TTY
I expect the time to be fetched from rails console and get displayed in shell terminal
I believe the best way is create a ruby script that run the ruby code that you want and then in your shell script you call it using rails runner, like this:
#!/bin/bash
rails runner script/myscript.rb
and in your script
puts Time.now.strftime("%d/%m/%Y %H:%M")

Edit bash PATH variable for Rails application

I have application minizinc in file ~/.bashrc, and I can call it on bash. I am building a Rails application that calls minizinc from bash, but I cannot do it. After executing this:
#cmd = ` bash -c "minizinc #{path} -n 1" `
I get the following error:
bash: minizinc: command not found
How can I change the Rails application user's PATH variable from the application? Or how do I tell the Rails application where this bash application is located?
You have several options here. The one I think best suits your case and would recommend is using the command directly, instead of calling Bash to do the same as Ruby:
#cmd = `minizinc #{path} -n 1`
If you use it like this, the command is executed in a shell with an environment similar to the one where Ruby is running. Which means that the PATH variable will be the same. So if the dir containing the executable minizinc is in PATH when you start the Rails server, it should also be in the PATH variable of the shell running that command.
Now, if you really need to use Bash in the middle, I strike it as odd that the PATH variable is not the same as in Ruby (I tried it using IRB and seems to work as expected). You can check it by replacing your command with
bash -c "echo $PATH"
It should print the same value as
puts ENV['PATH']
when run in the Rails console.
If, after checking it, you see that the PATH variable of your Rails environment is incorrect, you can set it specifically for the Rails server:
PATH="<path_to_minizinc_dir>:$PATH" rails server
This sets the value of the PATH environment variable only for the command you are about to execute, in this case rails server.
Alternatively, you can surpass all this by simply using the absolute path to the executable:
#cmd = `bash -c "/full/path/to/minizinc #{path} -n 1"`
If you provide the full path to the command you want to execute, the PATH environment variable simply won't come into play, but I imagine this would be suboptimal for your case.

How to make run external shell command in non-blocking mode in Ruby on Rails 3

I should change to specific folder so that the output of the script can be exported into a file in that folder (123.txt in the example code below).
but this command, which I run from inside my ruby code
./iw2_broadcast.py
takes 5 minutes or more to complete. I try to append & to make it run in the background, but it seems it does not work.
Any ideas? Thanks
Dir.chdir(#iw2_dir)
` ./iw2_broadcast.py -f 123.txt & `
puts "123"
Create a shell script with the execution command
# py.sh
./iw2_broadcast.py
Execute the above shell script from ruby using system command
# ruby_script.rb
system("./py.sh")
Now your ruby code will be executed without waiting for the output
Adding & works with system(). You can easily test it with:
system("sleep 1 &")
So for this precise example:
system("./iw2_broadcast.py -f 123.txt &")

Run rspec against vagrant box

I want to run the rspec tests for Gitlab within vim (calling: !rspec). The problem is, i'm using the vagrant box provided here: https://github.com/gitlabhq/gitlab-vagrant-vm
This means that the whole app is running inside of the vagrant box, and when i call rspec from within vim, it tries to run locally.
Is there any way i can forward the rspec call to the vagrant box, and get the output back locally?
I've found this one: https://github.com/clintoncwolfe/vagrant-rspec-ci But its not what im looking for, since i have to run a custom command.
Maybe its related to: Spork, Vagrant, and Rspec
You can execute any command over ssh, and return the output:
$ ssh user#host echo "hi"
hi
You could use this to run rspec:
$ ssh user#vagrant-ip "cd /path/to/project && rspec"
The thing to bear in mind with this approach is that your shell's environment must be set up correctly to allow access to the rspec command. Your mileage will vary depending on how you manage your rubies.
But you can set up a command in vim to execute this SSH command, and the output will be returned, e.g.:
:command VRspec exec "system('ssh user#vagrant-ip...')"

Ruby background process STDOUT is empty

I'm having a weird issue with a start-up script which runs a Sinatra script using the shell's "daemon" function. The problem is that when I run the command at the command line, I get output to STDOUT. If I run the command at the command line exactly as it is in the script -- less the daemon part -- the output is correctly redirected to the output file. However, when the startup script runs it (see below), I get stuff to the STDERR log but not to the STDOUT log.
The relevant lines of the script:
#!/bin/sh
# (which is and has been a symlink to /bin/bash
# Source function library.
. /etc/init.d/functions
# Set Some Variables
RUNAS="joeuser"
PID=/var/run/myapp.pid
LOG="/var/log/myapp/app-out.log"
ERR_LOG="/var/log/myapp/app-err.log"
APPLICATION_COMMAND="RAILS_ENV=production ruby /opt/myapp/lib/daemons/my-sinatra-app.rb -p 8002 2>>${ERR_LOG} >>${LOG} &"
# Snip a bunch. This is the applicable line from the "start" case:
daemon --user $RUNAS --pidfile $PID $APPLICATION_COMMAND &> /dev/null
Now, the funky parts:
The error log is written to correctly via the redirect of STDERR.
If I reverse the order of the >> and the 2>> (I'm grasping at straws, here!), the behavior does not change: I still get STDERR logged correctly and STDOUT is empty.
If the output log doesn't exist, the STDOUT redirect creates the file. But, the file remains 0-length.
This used to work. The log directory is maintained by log-rotate. All of the more-recent 'out' logs are 0-length. The older ones are not. It seems like it stopped working some time in April. The ruby code didn't change at any time near then; neither did the startup script.
We're running three different services in this way. Two of them are ruby daemons (one uses sinatra, one does not) and the other is a background java process. This is occurring for BOTH of the ruby processes but is not happening on the java process. Maybe something changed in Ruby?
FTR, we've got ruby 1.8.5 and RHEL 5.4.
I've done some more probing. The daemon function does a bunch of stuff, but the meat of the matter is that it runs the program using runuser. The command essentially looks like this:
runuser -s /bin/bash - joeuser -c "ulimit -S -c 0 >/dev/null 2>&1 ; RAILS_ENV=production ruby /opt/myapp/lib/daemons/my-sinatra-app.rb -p 8002 '</dev/null' '>>/var/log/myapp/app-out.log' '2>>/var/log/myapp/app-err.log' '&'"
When I run exactly that at the command line (both with and without the single-ticks that got added somewhere along the line), I get the exact same screwy behavior w.r.t. the output log. So, it seems to me that this is an issue of how ruby (?) interacts with runuser?
Too long to put in a comment :-)
change the shebang to add #!/bin/sh -x and verify that everything is expanded according to your expectations. Also, when executing from terminal, your .bashrc file is sourced, when executing from script, it is not; might be something in you're environment that differ. One way to find out is to do env from terminal and from script and diff the output
env > env_terminal
env > env_script
diff env_terminal env_script
Happy hunting...

Resources