How I can run 'cmd' command inside rake process using Rails 4 - ruby

I want to save data after running below command.
ip = '12.33.44.55' #it will be dynamic.
traceroute -q 1 -n ip
But problem is how can I run this command inside rake task.
Normally it runs in command line of Linux.
Any gem or ruby library to do this?
Any help please....

It possible to run any shell command with magic quote "`" in any ruby code.
ip = '12.33.44.55'
`traceroute -q 1 -n #{ip}`

Related

how to execute bash script with options in Robot Framework

I want to execute some kind of bash script in Robot Framework.
In terminal I use that command:
bash /home/Documents//script.sh --username=root --password=hello --host=100.100.100.100 --port=400 - --data='{"requestId":1,"parameters":{"name":"check","parameters":{"id":"myID"}}}'
and it works
In robot script I try with:
Running script
${result} = Run Process bash /home/Documents//script.sh "username\=root" "password\=hello" "host\=100.100.100.100" "port\=400" "data\='{"requestId":1,"parameters":{"name":"check","parameters":{"id":"myID"}}}'" shell=True stdout=stdout.txt
Log To Console ${result}
Log ${result}
Log ${result.stdout}
Log ${result.stderr}
But I get Missing required arguments: username, password, host, port.
Process doesn't recognise arguments.
How to pass script arguments in Robot Framework with Process Library?
Please show examples, I checked already doc in Process Library for Specifying command and arguments but I don't understand it.
After the night I found solution:
Running script
${result} = Run Process bash /home/Documents//script.sh username\=root password\=hello host\=100.100.100.100 port\=400 data\='{"requestId":1,"parameters":{"name":"check","parameters":{"id":"myID"}}}' shell=True stdout=stdout.txt
Options should be unquoted but = should be escaped with \

Running a JMeter script with nohup

For the first time I'm just playing around with nohup on top of an Ubuntu server. I read few docs about nohup and got to know about the running commands with options such as nohup ./server.sh &.
What I want to know is that, how should I be running the JMeter script (in headless mode) using nohup? Following is the script I needed to run with nohup:
./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx
When I tried using the normal nohup operation within the script it always throws me an error saying -n command not found. How should I move on with this? Any help would be appreciated.
Although I cannot reproduce your issue you can try surrounding your command with quotation marks like:
nohup "./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx"
Also don't forget -l key to save the results into a file.
The full command which runs script totally in the background will look like:
nohup "./jmeter.sh -n -t /home/chamith/WSO2MB/new/apache-jmeter-2.13/bin/GamesSubscriber.jmx -l result.jtl" > /dev/null 2>&1 &
References:
nohup man page
nohup Execute Commands After You Exit From a Shell Prompt
How Do I Run JMeter in Non-GUI Mode?
Full list of JMeter command-line options

Setting env variable to a cron scheduled task using whenever gem

In my code below, I wanted to set few environment variables stored in a file. Am I missing something? Printing env in production after 'bundle exec whenever' does not show the environment variables set. Using whenever gem for a scheduled cron task and spent hours figuring this. Any other way can be suggested too.
every 1.day, :at => '2:30 am' do
# Run shell script to assign variables and continue the rake task
system "for line in `cat config/myEnvFile.env` ; do export $line ; done"
rake "task:continue_doing_my_task"
end
system is not a whenever job type. It's Kernel.system, which executes the String being passed to it when the whenever command is run, rather than converting that String to cron syntax. It looks like what you really mean is:
command "for line in `cat config/myEnvFile.env` ; do export $line ; done"
# Note: command instead of system
command is a built-in job type defined by whenever here.
Each line of code inside the every-block runs as it's own command. If you run whenever (without any arguments, so it just displays what it would put in your crontab without actually modifying the crontab, and after making the correction I describe above), you'll see that the output is something like this:
30 2 * * * * /bin/bash -l -c 'for line in `cat config/myEnvFile.env` ; do export $line ; done'
30 2 * * * * /bin/bash -l -c 'cd /path/to/project && RAILS_ENV=production bundle exec rake task:continue_doing_my_task --silent > my_log_file.log 2&>1'
Notice 2 issues:
Firstly, these 2 commands have nothing to do with each other--they are run as 2 totally separate processes.
The first one is running in cron's default directory, which is probably not where config/myEnvFile.env is located.
To fix this, you need to merge everything into a single command. By using whenever's rake job type, you will end up in the right directory, but you still to export all those variables somehow.
One way to do this, is to rename the file .ruby-env and use rvm. rvm, in addition to managing ruby versions for you, will automatically load all environment variables defined in .ruby-env when you enter the directory.
If RVM is not an option for you, or you want something more lightweight, rename the file .env and use dotenv. Their README documents exactly how to use the gem, with or without Rails. Without Rails, it's this easy:
Add dotenv to your Gemfile
Make this change to your Rakefile:
require 'dotenv/tasks' # 1. require this file
namespace :task
task continue_doing_my_task: :dotenv do # 2. make :dotenv a prerequisite for your task

Repeating Bash Task using At

I am running ubuntu 13.10 and want to write a bash script that will execute a given task at non-pre-determined time intervals. My understanding of this is that cronjobs require me to know when the task will be performed again. Thus, I was recommended to use "at."
I'm having a bit of trouble using "at." Based on some experimentation, I've found that
echo "hello" | at now + 1 minutes
will run in my terminal (with and without quotes). Running "atq" results in my computer telling me that the command is in the queue. However, I never see the results of the command. I assume that I'm doing something wrong, but the manpages don't seem to be telling me anything useful.
Thanks in advance for any help.
Besides the fact that commands are run without a terminal (output and input is probably redirected to /dev/null), your command would also not run since what you're passing to at is not echo hello but just hello. Unless hello is really an existing command, it won't run. What you want probably is:
echo "echo hello" | at now + 1 minutes
If you want to know if your command is really running, try redirecting the output to a file:
echo "echo hello > /var/tmp/hello.out" | at now + 1 minutes
Check the file later.

Monitoring Ruby script, using Monit - Including RVM

Im using Monit to monitor a ruby script that uses Ruby daemons gem, which launches a separate process with PID - following the instructions from Monitor ruby processes with Monit
In order to execute the ruby script I need to include RVM in the Monit start and stop strings, so I have access to all the gems.
However when .monitrc executes I get the following error:
$rvm_path (/usr/local/rvm) does not exist./home/william/.rvm/scripts/rvm: line 174: rvm_is_a_shell_function: command not found
/home/william/.rvm/scripts/rvm: line 185: __rvm_teardown: command not found
'myserver_1' failed to start
Aborting event
I added PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm to the start and stop command strings to try and include RVM. However still it doesn't work
Config file .monitrc:
....
check process myserver_1
with pidfile /home/william/ruby/barclays/myapp.rb.pid
start = "/bin/bash -c 'PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm && ruby /home/william/ruby/barclays/daemonloader.rb start'"
stop = "/bin/bash -c 'PATH=$PATH:/home/william/.rvm/bin && . /home/william/.rvm/scripts/rvm && ruby /home/william/ruby/barclays/daemonloader.rb stop'"
....
Thanks for your help.
EDIT
Ive got a feeling the problem is related to environment variables. Quoting from this page
You should also know that for security reasons Monit purges the
environment and only sets a spartan PATH variable that contains /bin,
/usr/bin, /sbin and /usr/sbin. If your program or script dies, the
reason could be that it expects certain environment variables or to
find certain programs via PATH. If this is the case you should set the
environment variables you need directly in the start or stop script
called by monit.
Finally, Monit uses the system call execv to execute a program or a
script. This means that you cannot write shell commands directly in
the start, stop or exec statements. To do this, you must do as above;
start a shell and issue your commands there. For example:
start program = "/bin/bash -c 'my shell command && my other
command'"
Use this:
/path/to/rvm/bin/rvm in /path/to/project do ...
Replace the paths with proper directories for rvm and project and the ... with the commands to stop/start - try:
/usr/bin/env "HOME=/home/william rvm_path=/home/william/.rvm
/home/william/.rvm/bin/rvm in /home/william/ruby/project do
ruby daemonloader.rb start"
This command will load RVM, cd into project path, load ruby for this ruby and execute given command.
You could try something like this in Monit.
start = "/bin/su - william -c 'cd /home/william/ruby/project && ~/.rvm/bin/rvm default do bundle exec ruby daemonloader.rb start'"
This worked for me.
Mentioning the gemset and ruby source solves the problem for me.
start program = "/bin/bash -c 'cd /home/project_path && source /home/user/.rvm/environments/ruby-2.4.2#global && RAILS_ENV=production bundle exec rails s'"

Resources