Skip to content

Instantly share code, notes, and snippets.

Created April 13, 2015 21:00
Show Gist options
  • Save anonymous/2437fa18051428f3b781 to your computer and use it in GitHub Desktop.
Save anonymous/2437fa18051428f3b781 to your computer and use it in GitHub Desktop.
# Ruby version is 2.2.0
# I can use start or run to test the script below
# If i use 'run' i get the following errors
# If i use 'start' it gets detached from the console returning the pid which however is non-existent
$ ruby test.rb start|run
test.rb:8:in `initialize': Permission denied @ rb_sysopen - test.rb.time (Errno::EACCES)
from test.rb:8:in `new'
from test.rb:8:in `fwrite'
from test.rb:23:in `block (2 levels) in <main>'
from test.rb:22:in `loop'
from test.rb:22:in `block in <main>'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons/application.rb:265:in `call'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons/application.rb:265:in `block in start_proc'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons/application.rb:274:in `call'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons/application.rb:274:in `start_proc'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons/application.rb:295:in `start'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons/controller.rb:59:in `run'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons.rb:193:in `block in run_proc'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons/cmdline.rb:88:in `call'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons/cmdline.rb:88:in `catch_exceptions'
from /home/naftilos76/.rvm/gems/ruby-2.2.0/gems/daemons-1.2.2/lib/daemons.rb:192:in `run_proc'
from test.rb:21:in `<main>'
##########################RUBY SCRIPT STARTS HERE##############################
require 'daemons' # daemonize a script
require 'fileutils' # various file utils
########################################################################
# 'w' = overwrite data in the file
# 'a' = append data on a new line in the file
def fwrite(filename, data, mode)
f = File.new(filename, mode)
f.write(data)
f.close
end
########################################################################
# get script's full path
path = File.expand_path(".", File.dirname(__FILE__))
# Get self filename including full path
file = __FILE__
#Daemons.daemonize
Daemons.run_proc(file) do
loop do
fwrite("#{file}.time", Time.now.to_s, "a")
puts Time.now.to_s
exit(0) if File.exists?("#{file}.stop")
sleep 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment