File: sleeper

package info (click to toggle)
puppet 0.24.5-3%2Blenny2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 9,688 kB
  • ctags: 7,630
  • sloc: ruby: 97,655; sh: 721; lisp: 262; makefile: 167; xml: 122; python: 28
file content (67 lines) | stat: -rwxr-xr-x 1,316 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby -w

###
# sleep indefinitely as a debug

require 'getoptlong'

#-----------------------------------------------------------------
def daemonize
     outfile = "/tmp/sleeperout"
     if pid = fork()
        Process.detach(pid)
        sleep 1
        # verify that we didn't have any problems starting the daemon
        if FileTest.exists?(outfile)
            $stderr.puts "Sleeper failed: %s" % File.read(outfile)
            File.unlink(outfile)
            exit(14)
        else
            exit(0)
        end
     end
     Process.setsid
     Dir.chdir("/")
     begin
         $stdin.reopen "/dev/null"
         $stdout.reopen "/dev/null", "a"
         $stderr.reopen $stdin
     rescue => detail
        File.open(outfile, "w") { |f|
            f.puts detail
        }
        exit(12)
     end
end
#-----------------------------------------------------------------

debug = false

result = GetoptLong.new(
	[ "--debug",	"-d",			GetoptLong::NO_ARGUMENT ],
	[ "--help",		"-h",			GetoptLong::NO_ARGUMENT ]
)

result.each { |opt,arg|
	case opt
		when "--help"
			puts "There is no help yet"
			exit
		when "--debug"
			debug = true
		else
			raise "Invalid option '#{opt}'"
	end
}

trap(:INT) {
    exit
}

unless debug
	daemonize()
end

# Sleep for no more than two minutes
sleep 120
exit