Ruby daemon not working - ruby

I need to run a standalone ruby script as Unix (linux) daemon.
After running that daemon I need to run another Ruby method with it.
I installed the ruby-daemon gem on my machine by using the gem install daemon.
I did the test daemon program.
My test.rb file is :
module Test
def test_method
#s =" ITS WORKING !"
file=File.new("/home/username/test.txt", "w")
file.puts #s
file.close
end
end
My test_control.rb file is :
# this is myserver_control.rb
require 'rubygems' # if you use RubyGems
require 'daemons'
Daemons.run('test.rb')
After this I run the following command: ruby test_control.rb start
Now how can I check whether the daemon program has started properly?
How can I invoke a method with it?

Looks like the formatting on your post is way off, so hopefully someone can fix that, but I think the problem here is you're defining a module but not actually firing off the method you define.
The Daemons utility only executes the script provided. You should test that your "test.rb" file can be run on the command line directly before trying to diagnose what might be wrong with Daemons itself.
It may be as reworking test.rb:
module Test
def self.test_method
#s =" ITS WORKING !"
file = File.new("/home/username/test.txt", "w")
file.puts #s
file.close
end
end
Test.test_method
There are other ways to use Daemons where you pass it a module to run, but you're not using it that way.

Related

using SCL in the command call for Ruby script

My employer has Ruby 1.8.7 in the /usr/bin/ruby, and allows the usage of Ruby 2.4 only via SCL (sofoware collections).
Which means that when I run ruby, I need to use (from the RH6 shell) scl enablde ruby-24 'ruby foo.rb' when foo.rb
is the file name.
I want to enable the ruby call on the first execution line, i.e., instead of the Ruby code file looking like:
#!/usr/bin/ruby
puts "Hello world"
That the code will look
#!cmd
puts "Hello world"
Where the cmd is what calls via the scl and run the Ruby 2.4 for the puts command. I know that a wrapper file can be used. I want something in 1 file.
How about this:
#!/usr/bin/ruby
if RUBY_VERSION != "2.4.1"
exec "scl enable ruby-24; ruby __FILE__"
end
puts "Ruby Version: #{RUBY_VERSION}"

Requiring a Ruby Gem in Ruby Script breaks Cron Job Execution

I'm trying to run a cron job using Gems. I've installed ruby via RVM and when I require a gem it breaks the cron job. I've tried requiring two totally different gems, PG / Pry, and when I require either, the cronjob doesn't complete. Here is the "testing code" that works fine:
open('/home/log.log', 'a') do |f|
f.puts Time.now.to_s
end
Here is how I setup the cronjob:
* * * * * /usr/local/rvm/rubies/ruby-2.0.0-p247/bin/ruby /home/test1.rb
I can see new output every minute. And when I add a require gem line at the top, it then breaks, but only when run through cron:
require 'pg'
open('/home/log.log', 'a') do |f|
f.puts Time.now.to_s
end
The cronjob runs (I can see it execute in the sys log), but never completes (no output ever makes it into the text file). I've tried this on two separate servers one Debian, one CentOS, and both have the same issue. Oddly enough this only affects the cron job, if I run the same ruby file from console: /home/test1.rb it will work just fine.
Any help would be great.
You need to setup your crontab with rvm e.g:
rvm cron setup
With that rvm sets your environment variables in your crontab file
then you have a crontab file having this at the top:
PATH="/usr/local/rvm/gems/ruby-1.9.3-p194/bin:/usr/local/rvm/gems/ruby-1.9.3-p194#global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin:/usr/local/rvm/bin:/usr/local/rvm/gems/ruby-1.9.3-p194/bin:/usr/local/rvm/gems/ruby-1.9.3-p194#global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p194/bin:/usr/local/rvm/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/usr/local/rvm/gems/ruby-1.9.3-p194#global/"
rvm_env_string='ruby-1.9.3-p194'
rvm_path='/usr/local/rvm'
rvm_ruby_string='ruby-1.9.3-p194'
RUBY_VERSION='ruby-1.9.3-p194'
GEM_HOME='/usr/local/rvm/gems/ruby-1.9.3-p194'
GEM_PATH='/usr/local/rvm/gems/ruby-1.9.3-p194:/usr/local/rvm/gems/ruby-1.9.3-p194#global'
MY_RUBY_HOME='/usr/local/rvm/rubies/ruby-1.9.3-p194'
IRBRC='/usr/local/rvm/rubies/ruby-1.9.3-p194/.irbrc'
Then you can stick your crontask beneath it

Command line script that runs a method only when it is executed?

I am building a gem for command line use, and I am aware of the if __FILE__ == $0 method for determining whether the file being run is the current file (from Run code only if script called from the command line), however this doesn't work in my case. I have a module with an initialize function that I would like to run when the gem is called from the command line.
module MyApp
def self.initialize
# do command line stuff
end
def self.test
# run a rake test
end
end
MyApp::initialize
However, when running rake test it calls the initialize function which returns an error:
/Library/WebServer/Documents/myapp ❤ rake test
Options:
-v, --[no-]verbose Run verbosely
-h, --help Show this message
rake aborted!
Command failed with status (255): [ruby -I"lib" -I"/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.1.0/lib" "/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/gems/rake-10.1.0/lib/rake/rake_test_loader.rb" "test/test_myapp.rb" ]
/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `eval'
/Users/bbriggs/.rvm/gems/ruby-2.0.0-p195/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => test
(See full trace by running task with --trace)
I think this is because I am doing MyApp:initialize, because if I take that out of my code the rake test runs as expected, but the command line tool no longer works.
At the moment I am testing my app via bundle exec bin/myapp, and printing out the __FILE__ and $0 variables give me bin/myapp and path/to/lib/myapp.rb respectively, so I was wondering what the best way is to detect whether the module is being required or called directly. Am I even doing this right? I'm a bit of a Ruby newbie. :-)
Finally figured this out. Instead of running MyApp:initialize in lib/myapp.rb I put it in the bin/myapp file. This ensures that it is only run when the app is run from the command line and not when being tested via Rake or required by another script.

How do I configure ruby to enter the debugger on Ctrl-C (SIGINT)?

I'd like to enter the debugger upon typing ctrl-C (or sending a SIGINT). I have installed the debugger (I'm running Ruby 1.9.3) and verified that it works. I've added this to my setup files (this is for Padrino, but I assume it would be similar for Rails):
# file: config/boot.rb
Padrino.before_load do
trap("SIGINT") { debugger } if Padrino.env == :development
end
... but typing Ctrl-C does not invoke the debugger. In fact, if I replace debugger with puts "saw an interrupt!", typing Ctrl-C doesn't cause a print to happen either.
update
Following this suggestion from Mike Dunlavey, I tried explicitly calling catch Interrupt from within the debugger:
$ rdebug `which padrino` console
^Z^Z$HOME/usr/bin/padrino:9
require 'rubygems'
(rdb:1) catch Interrupt
Catch exception Interrupt.
(rdb:1) c
=> Loading development console (Padrino v.0.10.7)
=> Loading Application BlueDotAe
=> Loading Application Admin
irb(main):001:0> C-c C-c^C
irb(main):001:0>
No joy -- interrupt did not enter the debugger.
What am I missing?
If you want to trap SIGINT while running in the console, the short answer is: you cannot unless you monkey-patch IRB. Every Ruby app (whether padrino, or rails or whatnot) that uses the console will end up calling usr/lib/ruby/1.9.1/irb.rb, and in IRB.start, it does:
trap("SIGINT") do
irb.signal_handle
end
... just before entering the main loop. This will override any trap("SIGINT") you might have put in your startup code.
But if you want to trap SIGINT in a script file (for example, if you want to profile your code as described by Mike Dunlavey here), you can create a script file such as:
# File: profile_complex_operation.rb
trap("SIGINT") { debugger }
MyApp.complex_operation
and then invoke it as in:
$ ruby profile_complex_operation.rb
Now, when you hit ^C (or send SIGINT from another process), it will enter the debugger.
You may try to use GDB wrapper for Ruby (GitHub).
Install on Linux via:
sudo apt-get install gdb python-dev ncurses-dev ruby-rvm
gem install gdb.rb
Basic usage:
require 'gdb'
# create a new GDB::Ruby instance and attach it to
# pid 12345
gdb = GDB::Ruby.new(12345)
# print the (ruby) backtrace of the remote process
gdb.backtrace.each { |line| puts line }
# show the current local variables, and their values
p gdb.local_variables
# evaluate arbitrary ruby code in the remote process
p gdb.eval('%(pid #{$$})')
# show how many instances of each class exist in the
# remote process
p gdb.object_space
# raise an exception in the remote process
gdb.raise Exception, "go boom!"
# close the connection to the remote process
gdb.quit
Or to debug the hung process, attach it via:
rvmsudo gdb.rb PID
then:
# in gdb get a ruby stacktrace with file names and line numbers
# here I'm filtering by files that are actually in my app dir
(gdb) ruby eval caller.select{|l| l =~ /app\//}
Source: Using gdb to inspect a hung ruby process
Some alternatives:
rbtrace - like strace, but for ruby code (usage: rbtrace -p <PID> --firehose).
debug.rb script by tmm1 (author of gdb.rb) which can help to debug a process using strace/gdb.
See also:
Debugging Ruby Tools
Check why ruby script hangs

How would I modify this rake task to start a thin server with support for local debugging?

Question
I want to be able to debug my Sinatra website hosted by Thin on my local machine, and I want to be able to initiate this by using rake.
I'm not able to accept answers suggesting the use of different technologies (e.g. Windows, Rails, Java) or other servers (e.g. unicorn, passenger, puma); however, if what I'm asking for is impossible, then I will accept that answer.
What I've tried
My current Rakefile contains a task :start which starts the Thin server, but when it hits the breakpoint no output is displayed on my terminal. If I start Thin directly from the terminal, then I see the (rdb:1) prompt when it hits the breakpoint as expected. In either case, the Thin server is correctly running the site (confirmed by commenting out the breakpoint).
Gemfile
source :rubygems
gem 'sinatra'
gem 'thin'
gem 'debugger-pry'
Rakefile
task :start do
conf = File.expand_path('config.ru', File.dirname(__FILE__))
`thin -e development -R #{conf} --debug start`
end
config.ru
require File.expand_path('app', File.dirname(__FILE__))
run ModularExample::App.new
app.rb
require 'sinatra'
require 'debugger/pry'
module ModularExample
class App < Sinatra::Base
get '/' do
debugger
"Hello, world"
end
end
end
You're not outputting to STDOUT. The backticks execute the command and return the output as a string so you could do something like
puts `thin -e development -R #{conf} --debug start`
but you want to stream the processes output to STDOUT so you actually want to do:
task :start do
conf = File.expand_path('config.ru', File.dirname(__FILE__))
exec("thin -e development -R #{conf} --debug start")
end
Learn more about calling command line calls from this question.

Resources