Register the watch's process at the end of meddle.
[god.git] / lib / god / process.rb
blob98a301bca52be1e3c838576bef2a2a5be40b4b0e
1 module God
2   class Process
3     attr_accessor :name, :uid, :gid, :start, :stop, :restart, :pidfile
4     
5     def initialize(options={})
6       options.each do |k,v|
7         send("#{k}=", v)
8       end
9     end
10     
11     def start!
12       call_action(:start)
13     end
14     
15     def stop!
16       call_action(:stop)
17     end
18     
19     def restart!
20       call_action(:restart)
21     end
22     
23     def call_action(action)
24       command = send(action)
25       if command.kind_of?(String)
26         # string command
27         # fork/exec to setuid/gid
28         fork {
29           Process::Sys.setgid(Etc.getgrnam(self.gid).gid) if self.gid
30           Process::Sys.setuid(Etc.getpwnam(self.uid).uid) if self.uid
31           $0 = command
32           exec command
33         }
34       elsif command.kind_of?(Proc)
35         # lambda command
36         command.call
37       else
38         raise NotImplementedError
39       end
40     end
41   end
42 end