I'm trying to execute .sh file in rails index action.
I try exec"sh app/controllers/file.sh"
it's execute it in terminal then stop the server! I don't know why.
And I try output = system"sh app/controllers/file.sh" it's return true in the browser not what is in the .sh file
Thanks in advance :)
You'll want to look at the Open3 class, specifically capture2 or capture3:
require 'open3'
stdout, stderr, status = Open3.capture3("sh app/controllers/myscript.sh")
As you can see above capture3 will get you the stdout, stderr, and status of your script run. While capture2 will just return stdout and status. There are other useful functions in Open3 which are worth looking at.
To understand why exec and system are behaving the way they are, you can read this SO answer: Ruby, Difference between exec, system and %x() or Backticks
Related
I try to execute shell commands in rails using the following:
result = `which wkhtmltoimage-proxy`
but I always get back:
No such file or directory - which wkhtmltoimage-proxy
If I just type the command in my shell, everything works but not in the rails environment.
Doesn't matter which commands I try, none of the work.
Do I need to configure anything in rails?
I figured it out. I am using an IDE tool and didn't set the environment variables correctly. Anyways, the problem is solved now. Thanks for all your help!
Think of the system ticks (`) as executing in your rails directory (i.e. 'myapp')
So, if you're expecting to run this command in another directory, such as your home folder, you'll have to specify that
result = `cd ~ && which wkhtmltoimage-proxy`
If this is on Windows
This is spec. Ruby doesn't execute any child process in such case on
Windows, so, ruby cannot set any status to $?.
bugs.ruby-lang.org/issues/1690
I'd like to get data from an output when a system command is finished in Lua,
even while that command may take a few minutes to an end.
Obviously popen executes the command separately from the lua process.
Does anyone has an idea to solve this?
r = popen('command','r')
for line in r:lines() do
print(line)
end
If the command uses buffered output (the default) then there's nothing you can do. Some commands (e.g., cat -u) have an option to use unbuffered output, but they're rare.
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
In my app, a user can run a script, and if it is run in the terminal, it shows a nice progress bar, how would I replicate this in rails? I mean I could do like something between the two to communicate, but I would prefer if it would just show a "live" shell output.
Thanks :)
If your script is invoked with 2>&1 at the end, backticks invocation will return STDOUT & STDERR.
result = ‘ls 2>&1‘
I'm writing a quick Rails app and was wondering how I can validate the success an exec'd command. The two commands I'm running are and SVN update, and a cp from one directory to another.
If you use the Kernel.system() method it will return a boolean indicating the success of the command.
result = system("cp -r dir1 dir2")
if(result)
#do the next thing
else
# handle the error
There is a good comparison of different ruby system commands here.
How are you executing the external commands? The Ruby system() function returns true or false depending on whether the command was successful. Additionally, $? contains an error status.
Just to be pedantic, you can't validate an exec'd command because exec replaces the current program with the exec'd command, so the command would never return to Ruby for validation.
For the cp, at least, you would probably be better of using the FileUtils module (part of the Ruby Standard Library), rather than dropping to the shell.
As noted above, the $? predefined variable will give you the return code of the last command to be executed by system() or the backtick operator.
For SVN update, check the version number before and after the update.
svn_start_version = IO.popen("svn info").readlines[4]
`svn update`
svn_end_version = IO.popen("svn info").readlines[4]
if svn_end_version > svn_start_version
"success"
end
For the cp, you could do a filesize check on the original file being equal to the copied file.
source_file_size = IO.popen("du file1").readlines
`cp file1 file2`
dest_file_size = IO.popen("du file2").readlines
if dest_file_size == source_file_size
"success"
end