pax_global_header00006660000000000000000000000064141117473730014522gustar00rootroot0000000000000052 comment=1bcef7f1892b3cc148a968e08c710c5d5e225819 daemons-1.4.1/000077500000000000000000000000001411174737300131535ustar00rootroot00000000000000daemons-1.4.1/.gitignore000066400000000000000000000001671411174737300151470ustar00rootroot00000000000000html/* Gemfile.lock *.gem examples/*/*.log examples/*/*.txt examples/*/*.output coverage myserver*.rb HOWTO Release.md daemons-1.4.1/.travis.yml000066400000000000000000000005121411174737300152620ustar00rootroot00000000000000script: bundle exec rspec --format documentation language: ruby os: - linux dist: xenial osx_image: xcode12.2 rvm: - 2.0.0 - 2.1 - 2.2 - 2.3.1 - 2.4 - 2.5 - 2.6 - 2.7 - "3.0" matrix: include: - rvm: 2.4 os: osx - rvm: 2.5 os: osx - rvm: 2.6 os: osx - rvm: 2.7 os: osx daemons-1.4.1/Gemfile000066400000000000000000000001341411174737300144440ustar00rootroot00000000000000source 'https://rubygems.org' # Specify your gem's dependencies in handler.gemspec gemspec daemons-1.4.1/LICENSE000066400000000000000000000021001411174737300141510ustar00rootroot00000000000000Copyright (c) 2005-2021 Thomas Uehlinger, 2014-2016 Aaron Stone Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. daemons-1.4.1/README.md000066400000000000000000000115241411174737300144350ustar00rootroot00000000000000# Ruby Daemons [![Build Status](https://travis-ci.org/thuehlinger/daemons.svg?branch=master)](https://travis-ci.org/thuehlinger/daemons)[![Code Climate](https://codeclimate.com/github/acuppy/daemons/badges/gpa.svg)](https://codeclimate.com/github/acuppy/daemons)[![Test Coverage](https://circleci.com/gh/acuppy/daemons.svg?style=shield&circle-token=a4f96fd41f7682661d6543e30207427ac8870c0d)](https://circleci.com/gh/acuppy/daemons) Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be _run as a daemon_ and to be _controlled by simple start/stop/restart commands_. If you want, you can also use daemons to _run blocks of ruby code in a daemon process_ and to control these processes from the main application. Besides this basic functionality, daemons offers many advanced features like _exception backtracing_ and logging (in case your ruby script crashes) and _monitoring_ and automatic restarting of your processes if they crash. ## Basic Usage You can use Daemons in four different ways: ### 1. Create wrapper scripts for your server scripts or applications Layout: suppose you have your self-written server `myserver.rb`: ``` ruby # this is myserver.rb # it does nothing really useful at the moment loop do sleep(5) end ``` To use `myserver.rb` in a production environment, you need to be able to run `myserver.rb` in the _background_ (this means detach it from the console, fork it in the background, release all directories and file descriptors). Just create `myserver_control.rb` like this: ``` ruby # this is myserver_control.rb require 'daemons' Daemons.run('myserver.rb') ``` And use it like this from the console: ``` sh $ ruby myserver_control.rb start (myserver.rb is now running in the background) $ ruby myserver_control.rb restart (...) $ ruby myserver_control.rb stop ``` For testing purposes you can even run `myserver.rb` _without forking_ in the background: ``` sh $ ruby myserver_control.rb run ``` An additional nice feature of Daemons is that you can pass _additional arguments_ to the script that should be daemonized by seperating them by two _hyphens_: ``` sh $ ruby myserver_control.rb start -- --file=anyfile --a_switch another_argument ``` ### 2. Create wrapper scripts that include your server procs Layout: suppose you have some code you want to run in the background and control that background process from a script: ``` ruby # this is your code # it does nothing really useful at the moment loop do sleep(5) end ``` To run this code as a daemon create `myproc_control.rb` like this and include your code: ``` ruby # this is myproc_control.rb require 'daemons' Daemons.run_proc('myproc.rb') do loop do sleep(5) end end ``` And use it like this from the console: ``` sh $ ruby myproc_control.rb start (myproc.rb is now running in the background) $ ruby myproc_control.rb restart (...) $ ruby myproc_control.rb stop ``` For testing purposes you can even run `myproc.rb` _without forking_ in the background: ``` sh $ ruby myproc_control.rb run ``` ### 3. Control a bunch of daemons from another application Layout: you have an application `my_app.rb` that wants to run a bunch of server tasks as daemon processes. ``` ruby # this is my_app.rb require 'daemons' task1 = Daemons.call(:multiple => true) do # first server task loop do conn = accept_conn() serve(conn) end end task2 = Daemons.call do # second server task loop do something_different() end end # the parent process continues to run # we can even control our tasks, for example stop them task1.stop task2.stop exit ``` ### 4. Daemonize the currently running process Layout: you have an application `my_daemon.rb` that wants to run as a daemon (but without the ability to be controlled by daemons via start/stop commands) ``` ruby # this is my_daemons.rb require 'daemons' # Initialize the app while we're not a daemon init() # Become a daemon Daemons.daemonize # The server loop loop do conn = accept_conn() serve(conn) end ``` For further documentation, refer to the module documentation of Daemons. ## Displaying daemon status When daemonizing a process using a wrapper script, as examples 1 and 2 above, the status can be shown using ``` sh $ ruby myproc_control.rb status ``` By default this will display whether or not the daemon is running and, if it is, its PID. A custom message can be shown with ``` ruby def custom_show_status(app) # Display the default status information app.default_show_status puts puts "PS information" system("ps -p #{app.pid.pid.to_s}") puts puts "Size of log files" system("du -hs /path/to/logs") end Daemons.run('myserver.rb', { show_status_callback: :custom_show_status }) ``` ## Documentation Documentation can be found at . ## Author Written 2005-2021 by Thomas Uehlinger, 2014-2016 by Aaron Stone. daemons-1.4.1/Rakefile000066400000000000000000000005631411174737300146240ustar00rootroot00000000000000require 'rake' require 'rdoc/task' task :default do sh %{rake -T} end desc 'Create the RDOC html files' rd = Rake::RDocTask.new(:rdoc) do |rdoc| rdoc.rdoc_dir = 'html' rdoc.title = 'Daemons' rdoc.options << '--line-numbers' << '--main' << 'README.md' rdoc.rdoc_files.include('README.md', 'Releases', 'LICENSE') rdoc.rdoc_files.include('lib/**/*.rb') end daemons-1.4.1/Releases000066400000000000000000000217101411174737300146420ustar00rootroot00000000000000= Daemons Release History == Release 1.4.1: August 26, 2021 * Fix :proc mode (pass &block explicitly) (thanks to Graham Rogers) * Fix style of REAMDE.md == Release 1.4.0: May 1, 2021 * Allow for customization which signals are sent to stop process (thanks to philister) * Resolves mismatched indentations (thanks to Luis M Rodriguez) * Allow to use pry-byebug 3.8.0 (thanks to kamipo) == Release 1.3.1: December 14, 2018 * Fix undefined local variable or method `pid_delimiter' == Release 1.3.0: December 10, 2018 * Make logging more configurable. * Add configuration options for pid file delimters, force_kill_waittime * All status callback to be anything callable. == Release 1.2.6: December 24, 2017 * Add links to rubydoc.info documentation. == Release 1.2.5: October 22, 2017 * In Application#stop, call zap, not cleanup on the pidfile (thanks to wevanscfi) * Use File.expand_path on and output and log files (thanks to Dave Harris) == Release 1.2.4: August 1, 2016 * add :shush option * add :monitor_interval option * add :log_output_syslog option == Release 1.2.3: June 25, 2015 * fix: ApplicationGroup now waits on subprocesses in start_all (thanks to tobithiel) == Release 1.2.2: March 17, 2015 * fix 100% CPU usage bug when using monitor mode. == Release 1.2.1: March 10, 2015 * increase version number to be able to re-push to rubygems == Release 1.2.0: March 8, 2015 * add options for custum log file names. * change pid file name scheme to "#progname_num#{number}.pid" for multiple instances. * fix call_as_daemon not saving the PID (thanks Roberto Plancarte) * allow for custom statis messages (thanks to Joseph Haig) * fix Pid.running? rescuing timeout exceptions (thanks to Geraud Boyer) * monitor.rb/application.rb/application_group.rb: handle :monitor and :multiple in combination correctly (thanks to Prakash Murthy). * pidfile.rb: Handle invalid or empty pid files instead of returning pid 0 (thanks to Aaron Stone) * run the whole gem through Rubocop (thanks to Aaron Stone) * gem cleanup (thanks to Aaron Stone) == Release 1.1.9: August 10, 2012 * daemonize.rb: do srand in the forked child process both in daemonize and call_as_daemon (thanks to Andrew Havens). == Release 1.1.8: February 7, 2012 * rename to daemonization.rb to daemonize.rb (and Daemonization to Daemonize) to ensure compatibility. == Release 1.1.7: February 6, 2012 * start_proc: Write out the PID file in the newly created proc to avoid race conditions. * daemonize.rb: remove to simplify licensing (replaced by daemonization.rb). == Release 1.1.6: January 18, 2012 * Add the :app_name option for the "call" daemonization mode. == Release 1.1.5: December 19, 2011 * Catch the case where the pidfile is empty but not deleted and restart the app (thanks to Rich Healey) == Release 1.1.4: June 17, 2011 * Do not change the umask to 0000 when daemonizing anymore, just leave it as it was (thanks to Jon Botelho). == Release 1.1.3: April 14, 2011 * Fixed a bug in Application.stop: the cached pid number needs to be used to check for the status of a killed process (thanks to Jimmy Sieben). == Release 1.1.2: March 29, 2011 * Fixed gemspec to include all needed files. == Release 1.1.1: March 29, 2011 * Make the logging facilities work in :mode => :none (i.e. when calling Daemons.daemonize) (thanks to the input from Peter Hegedus). == Release 1.1.0: June 20, 2010 * Honour the options[:app_name] in Daemons.daemonize (thanks to Ryan Tecco). * Included a new option :stop_proc to specify a proc that will be called when a daemonized process receives a request to stop (thanks to Dave Dupre). * Only delete the pidfile if the current pid is the original pid (ghazel). * Start when restart but no application running (pcreux). * Silently continue if there is no pidfile (ghazel). * We now per default wait for processes to stop and kill them automatically it if they do not stop within a given time (force_kill_waittime). Use the option --no_wait to not wait for processes to stop. * Set log files mode to 0644 (mikehale). * Set pid file permissions to 0644 (mikehale). * Added ability to change process uid/gid (mikehale). * Fix for: If you happen to start a daemon from a process that has open file descriptors these will stay open. As it is daemonize.rb only closes ruby IO objects (thanks to Han Holl). * New reload command (SIGHUP) (thanks to Michael Schuerig). == Release 1.0.10: March 21, 2008 * By default, we now delete stray pid-files (i.e. pid-files which result for example from a killed daemon) automatically. This function can be deactivated by passing :keep_pid_files => true as an option. * All pid files of :multiple daemons new get deleted correctly upon exit of the daemons (reported by Han Holl). * Use the signal 'KILL' instead of 'TERM' on Windows platforms. * Use exit! in trap('TERM') instead of exit when option :hard_exit is given (thanks to Han Holl). * Did some clarification on the exception log. == Release 1.0.9: October 29, 2007 * fixed a severe bug in the new Pid.running? function: function returned true if the process did not exist (thanks to Jeremy Lawler). == Release 1.0.8: September 24, 2007 * new Pid.running? function. Checking whether a process exists by sending signal '0' (thanks to Dru Nelson). == Release 1.0.7: July 7, 2007 * Patch to fix wrong ARGV when using :exec (in def start_exec: Kernel.exec(script(), *(@app_argv || []))) (thanks to Alex McGuire). == Release 1.0.6: Mai 8, 2007 * New option to pass an ARGV-style array to run and run_proc (thanks to Marc Evans). * Additional patches for '/var/log' (thanks to Marc Evans). == Release 1.0.5: February 24, 2007 * Applied patch that makes daemons to use '/var/log' as logfile directory if you use :dir_mode = :system (thanks to Han Holl). * Daemons should now work with Ruby 1.9 (at least the basic features). == Release 1.0.4: January 17, 2007 * Document the :log_output option (thanks to Andrew Kuklewicz). * Set STDOUT.sync = true when redirecting to a logfile (thanks to Andrew Kuklewicz). * Should now run also correctly when there is no working 'ps ax' on the system (thanks to Daniel Kehoe). == Release 1.0.3: November 1, 2006 * Set the app_name correctly also for the monitor process (thanks to Ilya Novoselov). == Release 1.0.2: September 26, 2006 * Changed the 'ps -ax' call back to 'ps ax'. * Fixed the documentation for the :normal :dir_mode. * As a default for Daemons.run_proc, the pid file is now saved in the current directory. * In :ontop mode for running a proc (this is equal to calling something like 'ruby ctrl_proc.rb run'), the proc now runs directly in the calling script, not in a forked process anymore (thanks to Paul Butcher). * Set $0 to app_name in the daemons (thanks to Ilya Novoselov). == Release 1.0.1: August 30, 2006 * Fixed a regex for parsing the 'ps ax' system call. (thanks to Garance Alistair Drosehn) == Release 1.0.0: August 29, 2006 * Fix the parsing of the 'ps ax' system call. (thanks to Garance Alistair Drosehn) == Release 0.4.4: February 14, 2006 * Several fixes that allow us to use the Daemons::Controller with a proc instead of wrapping a script file. This gives us all the PID file management, monitoring, command line options, etc. without having to specify a path to our script which can be tricky, especially when using RubyGems. (thanks to John-Mason Shackelford) == Release 0.4.3: November 29, 2005 * New Option: You can specify the name of the application with :app_name on calling Daemons.run. This will be used to contruct the name of the pid files and log files. Defaults to the basename of the script. (thanks to Stephen R. Veit) * Bugfix: Handle the case where no controller options are given when calling Daemons, just options after "--". (thanks to Stephen R. Veit) == Release 0.4.2: November 15, 2005 * Bugfix for problem with :normal pid-file directory mode (pid.rb), fixed (thanks to Stephen R. Veit) == Release 0.4.1: September 11, 2005 * Bugfix for 'run' command line mode: didn't work anymore in 0.4.0, fixed == Release 0.4.0: July 30, 2005 * Two completely new operation modes: 1. Call a block as a daemon (Daemons.call { my_daemon_code }) and control it from the parent process. 2. Daemonize the currently running process (Daemons.daemonize) plus the already existing mode to control your scripts (Daemons.run("script.rb")) * Improved documentation (for example "How does the daemonization process work?") * Improved "simulation mode" (:ontop option) * Some minor bugfixes == Release 0.3.0: April 21, 2005 * New monitor functionality: automatic restarting of your applications if they crash * 'restart' command fixed * '--force' command modifier (please refer to the documentation) * Some more bugfixes and improvements == Release 0.2.1: Mar 21, 2005 * Bugfix for a problem with the 'status' command == Release 0.2.0: Mar 21, 2005 * Exception backtrace functionality added * Exec functionality added * More examples added * New commands: status, zap == Release 0.0.1: Feb 8, 2005 * Initial release daemons-1.4.1/daemons.gemspec000066400000000000000000000024631411174737300161530ustar00rootroot00000000000000require File.expand_path('../lib/daemons/version', __FILE__) Gem::Specification.new do |s| s.name = %q{daemons} s.version = Daemons::VERSION s.authors = ["Thomas Uehlinger"] s.license = "MIT" s.email = %q{thomas.uehinger@gmail.com} s.homepage = %q{https://github.com/thuehlinger/daemons} s.summary = %q{A toolkit to create and control daemons in different ways} s.description = <<-EOF Daemons provides an easy way to wrap existing ruby scripts (for example a self-written server) to be run as a daemon and to be controlled by simple start/stop/restart commands. You can also call blocks as daemons and control them from the parent or just daemonize the current process. Besides this basic functionality, daemons offers many advanced features like exception backtracing and logging (in case your ruby script crashes) and monitoring and automatic restarting of your processes if they crash. EOF s.metadata = { "documentation_uri" => "http://www.rubydoc.info/gems/daemons", } s.files = `git ls-files README.md LICENSE Releases lib examples`.split s.add_development_dependency 'rake', '~> 12.3', '>= 12.3.3' s.add_development_dependency 'rspec', '~> 3.1' s.add_development_dependency 'simplecov', '~> 0' s.add_development_dependency 'pry-byebug', '~> 3.0' end daemons-1.4.1/examples/000077500000000000000000000000001411174737300147715ustar00rootroot00000000000000daemons-1.4.1/examples/call/000077500000000000000000000000001411174737300157045ustar00rootroot00000000000000daemons-1.4.1/examples/call/call.rb000066400000000000000000000023011411174737300171400ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' testfile = File.expand_path(__FILE__) + '.log' # On the first call to , an application group (accessible by Daemons.group) # will be created an the options will be kept within, so you only have to specify # :multiple once. # options = { :app_name => 'mytask', # :ontop => true, :multiple => true } Daemons.call(options) do File.open(testfile, 'w') do |f| f.puts 'test' end loop { puts '1'; sleep 5 } end puts 'first task started' Daemons.call do loop { puts '2'; sleep 4 } end puts 'second task started' # NOTE: this process will exit after 5 seconds Daemons.call do puts '3' sleep 5 end puts 'third task started' puts 'waiting 20 seconds...' sleep(20) # This call would result in an exception as it will try to kill the third process # which has already terminated by that time; but using the 'true' parameter forces the # stop_all procedure. puts 'trying to stop all tasks...' Daemons.group.stop_all(true) puts 'done' daemons-1.4.1/examples/call/call_monitor.rb000066400000000000000000000022231411174737300207120ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' testfile = File.expand_path(__FILE__) + '.log' # On the first call to , an application group (accessible by Daemons.group) # will be created an the options will be kept within, so you only have to specify # :multiple once. # options = { # :ontop => true, :multiple => true, :monitor => true } Daemons.call(options) do loop { puts '1'; sleep 20 } end puts 'first task started' # NOTE: this process will exit after 5 seconds Daemons.call do File.open(testfile, 'a') do |f| f.puts 'started...' puts '2' sleep 5 f.puts '...exit' end end puts 'second task started' puts 'waiting 100 seconds...' sleep(100) # This call would result in an exception as it will try to kill the third process # which has already terminated by that time; but using the 'true' parameter forces the # stop_all procedure. puts 'trying to stop all tasks...' Daemons.group.stop_all(true) puts 'done' daemons-1.4.1/examples/daemonize/000077500000000000000000000000001411174737300167445ustar00rootroot00000000000000daemons-1.4.1/examples/daemonize/daemonize.rb000066400000000000000000000006521411174737300212470ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :log_output => true } testfile = File.expand_path(__FILE__) + '.txt' Daemons.daemonize(options) puts 'some output...' File.open(testfile, 'w') do |f| f.write('test') end daemons-1.4.1/examples/run/000077500000000000000000000000001411174737300155755ustar00rootroot00000000000000daemons-1.4.1/examples/run/ctrl_crash.rb000066400000000000000000000005671411174737300202560ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :log_output => true, :backtrace => true } Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options) daemons-1.4.1/examples/run/ctrl_custom_logfiles.rb000066400000000000000000000007111411174737300223430ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :log_output => true, :backtrace => true, :output_logfilename => "custom_output.txt", :logfilename => "custom_log.log" } Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options) daemons-1.4.1/examples/run/ctrl_exec.rb000066400000000000000000000005231411174737300200720ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :mode => :exec } Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options) daemons-1.4.1/examples/run/ctrl_exit.rb000066400000000000000000000005121411174737300201150ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { } Daemons.run(File.join(File.dirname(__FILE__), 'myserver_exiting.rb'), options) daemons-1.4.1/examples/run/ctrl_hanging.rb000066400000000000000000000007471411174737300205710ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { #:mode => :exec, :multiple => true, :no_pidfiles => true, :force_kill_waittime => 5 #:force_kill_waittime => -1 # do not wait before killing -9 } Daemons.run(File.join(File.dirname(__FILE__), 'myserver_hanging.rb'), options) daemons-1.4.1/examples/run/ctrl_keep_pid_files.rb000066400000000000000000000005341411174737300221120ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :keep_pid_files => true } Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options) daemons-1.4.1/examples/run/ctrl_monitor.rb000066400000000000000000000005361411174737300206410ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :monitor => true } Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options) daemons-1.4.1/examples/run/ctrl_monitor_multiple.rb000066400000000000000000000006131411174737300225500ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :multiple => true, :monitor => true, :log_output => true, } Daemons.run(File.join(File.dirname(__FILE__), 'myserver_crashing.rb'), options) daemons-1.4.1/examples/run/ctrl_monitor_nocrash.rb000066400000000000000000000005251411174737300223540ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :monitor => true } Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options) daemons-1.4.1/examples/run/ctrl_multiple.rb000066400000000000000000000005261411174737300210040ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :multiple => true } Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options) daemons-1.4.1/examples/run/ctrl_normal.rb000066400000000000000000000004521411174737300204370ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb')) daemons-1.4.1/examples/run/ctrl_ontop.rb000066400000000000000000000005231411174737300203050ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :ontop => true } Daemons.run(File.join(File.dirname(__FILE__), 'myserver.rb'), options) daemons-1.4.1/examples/run/ctrl_optionparser.rb000066400000000000000000000017051411174737300216760ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' require 'optparse' require 'logger' require 'ostruct' class MyApp < Logger::Application def initialize(args) super(self.class) @options = OpenStruct.new(:daemonize => true) opts = OptionParser.new do |opts| opts.banner = 'Usage: myapp [options]' opts.separator '' opts.on('-N', '--no-daemonize', "Don't run as a daemon") do @options.daemonize = false end end @args = opts.parse!(args) end def run Daemons.run_proc('myapp', :ARGV => @args, :ontop => !@options.daemonize) do puts "@options.daemonize: #{@options.daemonize}" $stdout.sync = true loop do print '.' sleep(2) end end end end myapp = MyApp.new(ARGV) myapp.run daemons-1.4.1/examples/run/ctrl_proc.rb000066400000000000000000000007301411174737300201110ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :multiple => false, :ontop => false, :backtrace => true, :log_output => true, :monitor => true } Daemons.run_proc('ctrl_proc.rb', options) do loop do puts 'ping from proc!' sleep(3) end end daemons-1.4.1/examples/run/ctrl_proc_multiple.rb000066400000000000000000000006111411174737300220220ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { :log_output => true, :multiple => true, } Daemons.run_proc('ctrl_proc_multiple.rb', options) do puts 'hello' sleep(5) puts 'done' end daemons-1.4.1/examples/run/ctrl_proc_rand.rb000066400000000000000000000011011411174737300211060ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' Daemons.run_proc('myscript') do loop do file = File.open('/tmp/myscript.log', 'a') file.write(Random.rand) # breaks without seeding # file.write(Random.new.rand) # works without seeding # file.write(rand) # also works, but this is Kernel.rand() so its different file.write("\n") file.close sleep 2 end end daemons-1.4.1/examples/run/ctrl_proc_simple.rb000066400000000000000000000005231411174737300214620ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' Daemons.run_proc('ctrl_proc_simple.rb') do loop do puts 'ping from proc!' sleep(3) end end daemons-1.4.1/examples/run/ctrl_slowstop.rb000066400000000000000000000006521411174737300210430ustar00rootroot00000000000000lib_dir = File.expand_path(File.join(File.dirname(__FILE__), '../../lib')) if File.exist?(File.join(lib_dir, 'daemons.rb')) $LOAD_PATH.unshift lib_dir else begin; require 'rubygems'; rescue ::Exception; end end require 'daemons' options = { #:force_kill_waittime => 40 #:force_kill_waittime => -1 # do not wait before killing -9 } Daemons.run(File.join(File.dirname(__FILE__), 'myserver_slowstop.rb'), options) daemons-1.4.1/examples/run/myserver.rb000077500000000000000000000004441411174737300200030ustar00rootroot00000000000000#!/usr/bin/env ruby # This is myserver.rb, an example server that is to be controlled by daemons # and that does nothing really useful at the moment. # # Don't run this script by yourself, it can be controlled by the ctrl*.rb scripts. loop do puts 'ping from myserver.rb!' sleep(3) end daemons-1.4.1/examples/run/myserver_crashing.rb000066400000000000000000000005531411174737300216570ustar00rootroot00000000000000# This is myserver.rb, an example server that is to be controlled by daemons # and that does nothing really useful at the moment. # # Don't run this script by yourself, it can be controlled by the ctrl*.rb scripts. loop do puts 'ping from myserver.rb!' puts 'this example server will crash in 10 seconds...' sleep(10) puts 'CRASH!' fail 'CRASH!' end daemons-1.4.1/examples/run/myserver_exiting.rb000066400000000000000000000001771411174737300215320ustar00rootroot00000000000000loop do puts 'ping from myserver.rb!' puts 'this example server will exit in 3 seconds...' sleep(3) Process.exit end daemons-1.4.1/examples/run/myserver_hanging.rb000077500000000000000000000006021411174737300214720ustar00rootroot00000000000000#!/usr/bin/env ruby # This is myserver.rb, an example server that is to be controlled by daemons # and that does nothing really useful at the moment. # # Don't run this script by yourself, it can be controlled by the ctrl*.rb scripts. trap('TERM') do puts 'received TERM' loop do puts 'hanging!' sleep(3) end end loop do puts 'ping from myserver.rb!' sleep(3) end daemons-1.4.1/examples/run/myserver_slowstop.rb000077500000000000000000000006151411174737300217550ustar00rootroot00000000000000#!/usr/bin/env ruby # This is myserver_slowstop.rb, an example server that is to be controlled by daemons # and that does nothing really useful at the moment. # # Don't run this script by yourself, it can be controlled by the ctrl*.rb scripts. trap('TERM') do puts 'received TERM' # simulate the slow stopping sleep(10) exit end loop do puts 'ping from myserver.rb!' sleep(3) end daemons-1.4.1/lib/000077500000000000000000000000001411174737300137215ustar00rootroot00000000000000daemons-1.4.1/lib/daemons.rb000066400000000000000000000345541411174737300157070ustar00rootroot00000000000000require 'optparse' require 'optparse/time' require 'daemons/version' require 'daemons/pidfile' require 'daemons/cmdline' require 'daemons/exceptions' require 'daemons/monitor' require 'daemons/application' require 'daemons/application_group' require 'daemons/controller' # All functions and classes that Daemons provides reside in this module. # # Daemons is normally invoked by one of the following four ways: # # 1. Daemons.run(script, options): # This is used in wrapper-scripts that are supposed to control other ruby scripts or # external applications. Control is completely passed to the daemons library. # Such wrapper script need to be invoked with command line options like 'start' or 'stop' # to do anything useful. # # 2. Daemons.run_proc(app_name, options) { (...) }: # This is used in wrapper-scripts that are supposed to control a proc. # Control is completely passed to the daemons library. # Such wrapper scripts need to be invoked with command line options like 'start' or 'stop' # to do anything useful. # # 3. Daemons.call(options) { block }: # Execute the block in a new daemon. Daemons.call will return immediately # after spawning the daemon with the new Application object as a return value. # # 4. Daemons.daemonize(options): # Daemonize the currently runnig process, i.e. the calling process will become a daemon. # # == What does daemons internally do with my daemons? # *or*:: why do my daemons crash when they try to open a file? # *or*:: why can I not see any output from the daemon on the console (when using for example +puts+)? # # From a technical aspect of view, daemons does the following when creating a daemon: # # 1. Forks a child (and exits the parent process, if needed) # 2. Becomes a session leader (which detaches the program from # the controlling terminal). # 3. Forks another child process and exits first child. This prevents # the potential of acquiring a controlling terminal. # 4. Changes the current working directory to "/". # 5. Clears the file creation mask (sets +umask+ to 0000). # 6. Closes file descriptors (reopens +$stdout+ and +$stderr+ to point to a logfile if # possible). # # So what does this mean for your daemons: # - the current directory is '/' # - you cannot receive any input from the console (for example no +gets+) # - you cannot output anything from the daemons with +puts+/+print+ unless a logfile is used # # == How do PidFiles work? Where are they stored? # # Also, you are maybe interested in reading the documentation for the class PidFile. # There you can find out about how Daemons works internally and how and where the so # called PidFiles are stored. # module Daemons require 'daemons/daemonize' # Passes control to Daemons. # This is used in wrapper-scripts that are supposed to control other ruby scripts or # external applications. Control is completely passed to the daemons library. # Such wrapper script should be invoked with command line options like 'start' or 'stop' # to do anything useful. # # +script+:: This is the path to the script that should be run as a daemon. # Please note that Daemons runs this script with load