yes. running ./my_code.rb is how I normally start the software. when
run without daemonizing of any sort, this software runs properly and to
completion. there are 2 modes: interactive and service mode. I'm
trying to make the service mode run as a daemon.
I can't get anything past the daemonize statement to run.
I understand that these things are 2 different actions. One does steps,
then deamonizes. the other daemonizes the whole bloody process. both
are behaving the same way, and seem to die at the same place (only 1 set
of environment output). If I could post the whole thing, I would. I
cannot.
···
On Fri, 14 Jul 2006, Rogue Amateur wrote:
#
##
# FileName: StrmSrvInteract.rb
# Date: 2006/05/10
require('SimStream')
require('daemonize')
require('socket')
require('syslog')
include Daemonize
#
# check if command line options. If so, use those and "run once"
# else, create listener socket thread, process on loop until
# listener socket send "stop"
#
$mode = nil # assume service mode
$mode = 1 if ARGV.length > 0
$rtr_ip = '127.0.0.1'
$rtr_usr = 'george'
$OutPort = 6001 # output port for sockets
$InPort = 6000 # input port for sockets
$host = 'localhost'
$debug = nil
$sim = nil
# use syslog to properly add log messages to the log
$log = Syslog.open("my_code")
$log.debug("Running my_code.")
# use alt_log to hold debug comments if any
alt_log = "#{Dir.pwd}/my_codeDebug.log"
simple_socket = "Usage: via localhost:16000, send message as " + \
"\t[actionCode]\\0[inputValue(s)...]\\0\n" + \
"Return messages to localhost:16001"
simple_usage = "Usage: #{$0} [d[:[+]sim]] [v|h|c:cmd] [s:MCA]
[p:port]\n" + \
"\tUse #{$0} v to get version\nUse #{$0} h to get extended
help."
##
# FunctionName: process_request
# proprietary code I can't release. It never gets a chance to execute
this anyway.
##
# FunctionName: main
#
begin
# parse the arguments, if any, and see if in sim mode.
my_args = Hash.new
ARGV.each do |arg|
if arg =~ /
(key, value) = arg.split(":")
my_args[key] = value
else
my_args[arg] = ""
end
end
# allow for ip as an input parameter no matter what the mode.
if my_args.keys.length == 1 and my_args.has_key?("ip")
File.open(alt_log,"a+") { |f| f << "New IP for Router:
#{my_args["ip"]}"} # if $debug
$rtr_ip = my_args["ip"]
$mode = nil
elsif my_args.has_key?("ip")
File.open(alt_log,"a+") { |f| f << "New IP for Router:
#{my_args["ip"]}"} # if $debug
$rtr_ip = my_args["ip"]
end
# for debug to force in socket mode
$sim = 1
$debug = 1
# log the environment, I hope...
show_env = "\n"
ENV.each_pair { |k,v| show_env += "\t#{k}: #{v}\n" }
environment = "Dir: #{Dir.pwd}\nEff UID: #{Process.euid}\n" + \
"Eff GID: #{Process.egid}\nENV: #{show_env}\n"
File.open("#{Dir.pwd}/Environment.log","a+") { |f| f << environment }
# otherwise, daemonize and move on.
daemonize()
#####IT NEVER DOES THIS STEP. AT ALL.
# log the environment, I hope...
show_env = "\n"
ENV.each_pair { |k,v| show_env += "\t#{k}: #{v}\n" }
environment = "Dir: #{Dir.pwd}\nEff UID: #{Process.euid}\n" + \
"Eff GID: #{Process.egid}\nENV: #{show_env}\n"
File.open("/home/RA/Environment.log","a+") { |f| f << environment }
File.open(alt_log,"a+") { |f| f << "Debug mode on." } if $debug
File.open(alt_log,"a+") { |f| f << "Sim mode on." } if $debug and $sim
File.open(alt_log,"a+") { |f| f << "Socket processing." } if $debug
puts "Debug mode on." if $debug
puts "Sim mode on." if $debug and $sim
puts "Socket processing." if $debug
# open the router and get stream objects
puts "Opening router <#{Time.now}>..." if $debug
if $sim
$my_rtr = SimRouter.new($rtr_ip, $rtr_usr) # for form sake
$high_Def = SimStream.new($my_rtr.open, "highdef")
$low_Def = SimStream.new($my_rtr.shell, "lowdef")
else
begin
$my_rtr = Router.new($rtr_ip, $rtr_usr)
my_shell = $my_rtr.open
rescue
msg = "Could not open the real router. Error: <#{$!}>\n"
msg += "Pls verify router settings: RouterIP: #{$rtr_ip} User:
#{$rtr_usr}\n"
puts(msg)
$log.err(msg)
UDPSocket.open.send("510\000Stop\0001\000", 0, $host, $OutPort)
exit 1
end
begin
$high_Def = Stream.new(my_shell, "highdef")
rescue
msg = "could not determine HighDef contents. Message returned was:
<#{$!}>\n"
msg += "Pls verify router settings: RouterIP: #{$rtr_ip} User:
#{$rtr_usr}\n"
$log.err(msg)
$my_rtr.close
UDPSocket.open.send("510\000Stop\0001\000", 0, $host, $OutPort)
exit 1
end
begin
$low_Def = Stream.new(my_shell, "lowdef")
rescue
msg = "could not determine LowDef contents. Message returned was:
<#{$!}>\n"
msg += "Pls verify router settings: RouterIP: #{$rtr_ip} User:
#{$rtr_usr}\n"
$log.err(msg)
$my_rtr.close
UDPSocket.open.send("510\000Stop\0001\000", 0, $host, $OutPort)
exit 1
end
end
puts "done opening router <#{Time.now}>" if $debug
# assume socket-based service
server = UDPSocket.open
server.bind(nil, $InPort)
stop_stat = 1
last_word = ""
# do this just once, to let gvpmanager know you're up.
UDPSocket.open.send("520\0", 0, $host, $OutPort) if !$mode
while stop_stat
my_reply, my_from = server.recvfrom(64)
if my_reply =~ /^510/
# stop if told to stop
stop_stat = nil
last_word = my_reply
puts "Told to stop <#{Time.now}>..." if $debug
elsif my_reply =~ /^505/
### the rest of the acting code has been deleted to protect the guilty.
### Please understand that it never does the above noted steps.
# process_request
# output to socket
end
# do the last clean up pieces for the socket based mode.
File.open(alt_log,"a+") { |f| f << last_word } if !$mode and $debug
UDPSocket.open.send("#{last_word}0\0", 0, $host, $OutPort) if !$mode
msg = "Socket Service stopped. Please see system log for any error
messages."
puts msg if !$mode
rescue RuntimeError, StandardError => boom
msg = "Runtime processing error. Software threw an error <#{boom}>."
$log.err(msg)
puts (msg) if $mode
if defined? $my_rtr and !$my_rtr.nil?
$my_rtr.close
end
UDPSocket.open.send("510\000Stop\0001\000", 0, $host, $OutPort) if
!$mode
exit 1
rescue Exception => e
require 'tmpdir'
tmp = Dir.tmpdir
this = File.basename $0
pid = Process.pid
deathlog = File.join tmp, "#{ this }.#{ pid }"
m, c, b = e.message, e.class, e.backtrace.join("\n")
msg = "%s (%s)\n%s" % [m, c, b]
open(deathlog, "w"){|f| f.puts msg}
end
--
Posted via http://www.ruby-forum.com/\.