Skip to content

Instantly share code, notes, and snippets.

@iDiogenes
Created January 13, 2011 18:26
Show Gist options
  • Save iDiogenes/778342 to your computer and use it in GitHub Desktop.
Save iDiogenes/778342 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# Daemon script for running ifs_notify
# The script can start, stop and check status of ifs_notify
pwd = Dir.getwd
file = pwd + "/ifs_notify"
pidfile = "../var/ifs_notify.pid"
# Starting ifs_notify
def start(pidfile, file)
if File.exists? pidfile
begin
mypid = File.open(pidfile).readline
proc_check = Process.kill(0, mypid.to_i) # check is process ID found in file is running
rescue
File.delete(pidfile) # pid was not found so delete the file and start the program
end
if proc_check == 1
puts "ifs_notify already running as PID #{mypid}, ifs_notify will not start\n"
exit 1
end
end
pid = fork do
puts "Starting ifs_notify with PID #$$\n"
File.open("../var/ifs_notify.pid", 'w') {|f| f.write("#$$") }
exec file
sleep
end
Process.detach(pid)
end
# Checking status of ifs_notify --- Need to add part about check if socket is open copy from above
def status(pidfile)
if File.exists? pidfile
mypid = File.open(pidfile).readline
puts "ifs_notify is running, process ID is #{mypid}\n"
else
puts "ifs_notify is not running\n"
end
end
# Stopping ifs_notify
def stop(pidfile)
if File.exists? pidfile
mypid = File.open(pidfile).readline
Process.kill("TERM", mypid.to_i)
File.delete(pidfile)
puts "ifs_notify running as PID #{mypid} has been shutdown\n"
else
puts "Cannot find pid for ifs_notify\n"
end
end
# Parse through user cmdline input
if ARGV[0] == "start"
start(pidfile, file)
elsif ARGV[0] == "status"
status(pidfile)
elsif ARGV[0] == "stop"
stop(pidfile)
else
puts "Please select an option, choices are start|status|stop\n"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment