Launch a script on a separate server from a Rails app
I use ssh for these types of things.
require 'net/ssh'
Net::SSH.start('server.com', 'username', password: "asdasd") do |ssh|
$stdout.print ssh.exec!("cdc && curl https://gist.github.com/mhenrixon/asdasd123123/raw/123123asdasd/update.rb | rails c production")
end
That's the easiest way of doing it I think but the sinatra/rails listener isn't a bad idea either.
serve current directory from command line
Simplest way possible (thanks Aaron Patterson/n0kada):
ruby -run -e httpd . -p 9090
Alternate, more complex way:
ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 9090, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"
Even the first command is hard to remember, so I just have this in my .bashrc
:
function serve {
port="${1:-3000}"
ruby -run -e httpd . -p $port
}
It serves the current directory on port 3000 by default, but you can also specify the port:
~ $ cd tmp
~/tmp $ serve # ~/tmp served on port 3000
~/tmp $ cd ../www
~/www $ serve 5000 # ~/www served on port 5000
Writing a simple webserver in Ruby
The problem appears to be in the child script, since the parent script in your question runs on my box (Debian Squeeze, Ruby 1.8.7 patchlevel 249):
I created the dummy child script bar.rb:
#!/usr/bin/ruby1.8
s = $stdin.read
$stderr.puts s
print s
I then ran your script, passing it the path to the dummy script:
$ /tmp/foo.rb /tmp/bar.rb
The I hit it with wget:
$ wget localhost:8080/index
And saw the dummy script's output:
GET /index HTTP/1.0^M
User-Agent: Wget/1.12 (linux-gnu)^M
Accept: */*^M
Host: localhost:8080^M
Connection: Keep-Alive^M
^M
I also saw that wget received what it sent:
$ cat index
GET /index HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: localhost:8080
Connection: Keep-Alive
It worked the same no matter how many times I hit it with wget.
Rails: Running Multiple Ruby Scripts on a Server
The link in the comments to another question (by Smar, thanks):
http://railscasts.com/episodes/127-rake-in-background
seemed to work fine for me. I didn't need Foreman or any other tool.
I just needed to add this to the Rakefile:
desc "Start some other jobs"
task :start_other_jobs do
system "ruby job1.rb &"
system "ruby job2.rb &"
end
(note the ampersand to make it run as a background task)
and then start it by
rake start_other_jobs
Easy, isn't it? :D
How can I run a ruby script right after the server starts in Rails 5
Have you tried Foreman gem? It will allow you to create a simple file (Procfile) where you can specify all the process that should be started simultaneously.
I usually create a file named Procfile.dev in the project's root, that would look like for example:
web: bundle exec rails server thin start -p 4000
mail: mailcatcher -f
your_script: instructions
Then you start your Rails app as:
foreman start -f Procfile.dev
With that command, Foreman will execute all the processes on the file.
You should install the gem locally and not in the Gemfile.
Foreman Gem
Related Topics
Can't All or Most Cases of 'Each' Be Replaced with 'Map'
Using Watir to Check for Bad Links
Is It Acceptable Having Parameter in Class Constructor
Cattr_Accessor Outside of Rails
Ruby Error Reading in Certificate File with Openssl
What's Wrong with the Square and Rectangle Inheritance
Ruby How to Merge Two CSV Files with Slightly Different Headers
How to Include Unit Tests in a Ruby Module
Rails 3 Caching: Expire Action for Named Route
Markdown to Plain Text in Ruby
When to Use Keyword Arguments Aka Named Parameters in Ruby
Ruby on Rails: Converting "Somewordhere" to "Some Word Here"
Can't Find Gem Railties (>= 0.A) with Executable Rails (Gem::Gemnotfoundexception)
Single Table Inheritance to Refer to a Child Class with Its Own Fields