The following command (and variations of it) run okay in terminal in mac, run okay in a ruby program executed on a mac, run okay directly in the windows command prompt, but fail with a parsing error when I try to run it inside of a ruby file on windows.
curl -u"user:pwd" -d"{\"name\":\"new_repo_beepo\"}" https://api.github.com/user/repos --insecure
I've tried executing it with backticks, %x() and system. I've also tried substitution of strings and json'ing pieces of it, without any luck. From what I can determine, the failure point is in the -d"{\"name\":\"repo_name\"}" section, but that's only from trying the command without it. Regardless, in each variation of the command on windows in ruby, I get a JSON parsing error.
Have you tried using rest-client?
It is a gem and works quite nice. It is probably better than using system()or %x() and it is definitely more secure (You can inject malicious bash commands on system() so it needs to be used carefully).
It is quite simple to use. Just install the gem and require it one your ruby file.
NOTE: If using Rails just add it to your Gemfile(No need to require it on each file).
require 'rest_client'
RestClient.get 'https://api.github.com/user/repos', {params: {id: 50, foo: 'bar'}}
You can also use some params for --insecure ssl.
It seems likely this is a parsing/quoting problem with the shell mechanism used by ruby to run the command on windows. Have you tried the tokenized form of system, eg:
system('curl', '-u"user:pwd"', '-d"{"name":"new_repo_beepo"}"', 'https://api.github.com/user/repos', '--insecure')
On posix that will send the arg vector as specified directly to the exec'ed program without letting the shell get in the way; probably the same semantics hold for windows.
You shouldn't need to exec curl to do this as ruby has stdlib Net::HTTP. In comments you mentioned that you've had problems with this module under jruby, but we have jruby services here exercising http[s] requests just fine, so you might try posting a question addressing the specific problems you have with jruby and native http client libs.
Related
I've tried using Ruby 2.0 x64 and Ruby 1.9.3 for Windows using RubyInstaller. Entering ruby -v works as expected, and running gem gives me the expected usage docs. Running and using the Interactive Ruby application works as expected. I am running Windows 8.1 Update.
However, for both installations, running ruby from cmd gives me a blank prompt where I can type, but nothing is executed when I press enter. If I attempt to install a gem, there is a similar issue where the program is running, but there is absolutely no output, and nothing happens.
I can't seem to be able to find a similar issue elsewhere. Does anyone know what might be wrong, and how I could fix it?
What did you expect to happen? ruby.exe is the ruby interpreter, meant for running ruby scripts. Normally, to use it you would create a file containing valid ruby commands with your favorite text editor (but not a word processor). If you save the file as foobar.rb, typing ruby foobar.rb (or if you told the installer to associate .rb files with ruby, typing just foobar.rb) will execute the commands in the file as a script/program. If you don't supply a script file name, ruby goes into input mode and expects you to type in a program on the spot. It won't give any feedback until you indicate end-of-file by typing CTRL-z, at which point it will process what you typed and most likely tell you about all the errors you made. If you want line-by-line interactive feedback, use irb.
We have a rails sidekiq setup to run jobs.
I am trying to make a job as portable as possible by separating the actual script from the sidekiq call.
So sidekiq calls a small stub job that backtick calls the actual script and captures the output.
That part works fine. But sidekiq runs the job as root, rather than as the user who's environment has all the rvm parts, rubies, and gemsets. I'd like to use the existing user rvm rubies and gemsets.
in the calling script (sidekiq job):
output = `source /path/to/dudes/rvm/environment.file && rvm use \
2.0.0-pxxx#default do ruby /path/to/actual/script.rb`
and the called script gets run, but as root and it obviously doesn't work as I intended because my requires are not found.
If I take that same command string and run it as a local user from BASH, who also has no gemsets, it seems to work.
I've tried just backtick calling it like a shell script
output = `/path/to/actual/script.rb`
...and in the called script, various combinations of shebangs.
#!/usr/bin/env ruby
#!/usr/bin/env rvm use everything i found on the internets
Now, I've gotten ruby scripts to run in environments with linux upstart jobs by using bash script wrappers like this:
http://techhelplist.com/index.php/tech-tutorials/43-linux-adventures/85-upstart-ruby-job-with-rvm
But I am trying to find a way to do this with no wrappers. Is it possible?
No, it is not possible.
When you use the "backtick" operator you are essentially calling Kernel#system(...). This means the command you execute is run in a subshell which cannot change the environment of the parent process in the ways you want.
See this question for comparison: How to run code after ruby Kernel.exec
Also consider reading more about the UNIX process model.
According to Upstart Intro, Cookbook and Best Practises, point 4.2.2, you can set user jobs in $HOME/.init/.
I have a ruby program named options that I want to run from the command line with a few options like
options -add 400
options -sub 600
options -h
I am already using optparse to interpret the commands but I want to run the program as its own script, but have to run it as
ruby options -add 400
I've looked it up and found some answers like How to create a shell command supporting Ruby? which seems to be linux and I'm not sure of part of the explanation like which bin to put in, or answers like Shell execute from Ruby whose answer still requires ruby in the command. Can anyone explain how to do this in more depth, or direct me to a source that explains it without relying on a linux platform?
Assuming that you have ruby in your PATH on Windows couln't you just create wrapper script and place it in folder present at your PATH?
ruby options %*
Do you have thought about to make it as gem ? There is a simple guide http://guides.rubygems.org/, then you can make the gem which will be working on linux and windows too. I don't know if you know but when I did software avaible under shell or bash I used slop (https://github.com/injekt/slop) it is very convenient the gem. Please see the source of slop, because it is an example of gem, all structure and necessary files to build the gem.
I want to run a Ruby script in Geektool that refreshes every 3 hours (so I set the refresh rate to 10,800 seconds) and the shell command in Geektool has this code in it:
ruby "/file.rb"
The file is located at root for convenience. Problem is, it won't run. I tried different commands, such as:
/Users/userhere/.rvm/rubies/ruby-1.9.3-p0/bin/ruby /file.rb
But it still doesn't work. I don't want it to use my /usr/bin/ruby installation (which, by default, is 1.8.7), I want it to use 1.9.3. So doing: /usr/bin/ruby "/file.rb" won't work for me.
In Terminal, if I run any of those commands they all work (except for the latter, because of dependencies) and my script works fine, but Geektool fails to even execute it. I tried with and without double quotes around the file name, even single quotes don't work.
Any help would be greatly appreciated.
I needed an rvm gemset as well; I created a shell file:
/Users/Dave/.rvm/bin/rvm use 1.9.3-p0 > /dev/null
/Users/Dave/.rvm/rubies/ruby-1.9.3-p0/bin/ruby ~/foo.rb
With that shell file (which happens to reside in a directory off my ~) as the shell command it works fine.
Without the full paths to each command it doesn't work. GeekTool doesn't run your .bash_profile AFAICT. Also not sure that running an rvm Ruby w/o using it would do what you want anyway.
I am using Sphinx quite often. There is one index that calls a stored procedure with one param as input. The param can be any number from 1 to 10 and each returs different results. Since it would make sphinx config quite crowded, even with inheritance. So I thought I will use shebang line at the start of sphinx config file (stored as sphinx.py now). This works great in production enviroment since it runs on Ubuntu. But I want to run it on my local machine as well, but here is the problem called - Windows. Since I have cygwin as well, I tried to run it via cygwin, but it is the same - nothing happens.
I tried to run with both cygwin paths and windows paths, but both get ignored or treated as comments. From what I have read it should be working with cygwin. Could it be that it does not work since I have to call an exe file?
With:
$ ./indexer.exe sphinx.conf
I have tried to run it as perl script, bash script (via cygwin) and it gets ignored either way.
Is there a reliable way to run shebang lines on Windows? Or force cygwin to at least spit an error in my face... Even hacks are good since its just my development machine.
Any help is appreciated
All a shebang line does is tell the unix system() call what interpreter to use. If you specify indexer.exe then you are saying that you want it to use indexer.exe, so that is what it will use.
If you run Indexer.exe, indexer.exe will decide what to do.
Does Indexer.exe understand shebang lines? Or not?
Perl, as a convenience, will read the shebang line, and if it isn't Perl, it will and call the other program for you.
So maybe call Perl instead of Indexer, and it will do the right thing?