Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Public Attributes
MoinMoin.util.daemon.DaemonScript Class Reference
Inheritance diagram for MoinMoin.util.daemon.DaemonScript:
Inheritance graph
[legend]
Collaboration diagram for MoinMoin.util.daemon.DaemonScript:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def run
def usage
def help
def do_start
def do_stop
def do_kill
def do_restart
def status
def readPID
def daemonize
def writePID
def removePID
def warn
def log

Public Attributes

 name
 function
 args
 kw
 pidFile

Static Public Attributes

string commandPrefix = 'do_'

Detailed Description

A script controlling a daemon

TODO: add --pid-dir option?

Definition at line 222 of file daemon.py.


Member Function Documentation

def MoinMoin.util.daemon.Daemon.daemonize (   self) [inherited]
Make the current process a daemon

See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16

Definition at line 183 of file daemon.py.

00183 
00184     def daemonize(self):
00185         """ Make the current process a daemon
00186 
00187         See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16
00188         """
00189         if os.fork():   # launch child and...
00190             os._exit(0) # kill off parent
00191         os.setsid()
00192         if os.fork():   # launch child and...
00193             os._exit(0) # kill off parent again.
00194         os.umask(0077)
00195         null = os.open('/dev/null', os.O_RDWR)
00196         for i in range(3):
00197             try:
00198                 os.dup2(null, i)
00199             except OSError, e:
00200                 if e.errno != errno.EBADF:
00201                     raise
00202         os.close(null)

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.do_kill (   self) [inherited]
Kill the daemon process

Kill or raise an error which we can't handle here. Clean the
pid file for the killed process.

Definition at line 112 of file daemon.py.

00112 
00113     def do_kill(self):
00114         """ Kill the daemon process
00115 
00116         Kill or raise an error which we can't handle here. Clean the
00117         pid file for the killed process.
00118         """
00119         running, pid = self.status()
00120         if not running:
00121             return self.log("%s is not running" % self.name)
00122         os.kill(pid, signal.SIGKILL)
00123         self.removePID()

Here is the call graph for this function:

def MoinMoin.util.daemon.Daemon.do_restart (   self) [inherited]
stop, wait until pid file gone and start again 

Definition at line 124 of file daemon.py.

00124 
00125     def do_restart(self):
00126         """ stop, wait until pid file gone and start again """
00127         running, pid = self.status()
00128         if not running:
00129             self.log("%s is not running, trying to start" % self.name)
00130         else:
00131             self.do_stop()
00132         timeoutSeconds = 2.0
00133         start = time.time()
00134         while time.time() - start < timeoutSeconds:
00135             running, pid = self.status()
00136             if not running:
00137                 break
00138             time.sleep(0.1)
00139         else:
00140             raise Error("could not start after %s seconds" % timeoutSeconds)
00141         self.do_start()

Here is the call graph for this function:

def MoinMoin.util.daemon.Daemon.do_start (   self) [inherited]
Start the daemon process

Start will daemonize then block until the server is killed and
then cleanup the pid file on the way out.

Definition at line 84 of file daemon.py.

00084 
00085     def do_start(self):
00086         """ Start the daemon process
00087 
00088         Start will daemonize then block until the server is killed and
00089         then cleanup the pid file on the way out.
00090         """
00091         running, pid = self.status()
00092         if running:
00093             raise Error("another application is running with pid %s "
00094                         "(try restart)" % pid)
00095         self.daemonize()
00096         self.writePID()
00097         try:
00098             self.function(*self.args, **self.kw)
00099         finally:
00100             self.removePID()

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.do_stop (   self) [inherited]
Stop the daemon process

Terminate or raise an error we can't handle here. On success,
the pid file will be cleaned by the terminated process.

Definition at line 101 of file daemon.py.

00101 
00102     def do_stop(self):
00103         """ Stop the daemon process
00104 
00105         Terminate or raise an error we can't handle here. On success,
00106         the pid file will be cleaned by the terminated process.
00107         """
00108         running, pid = self.status()
00109         if not running:
00110             return self.log("%s is not running" % self.name)
00111         os.kill(pid, signal.SIGINT)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 249 of file daemon.py.

00249 
00250     def help(self):
00251         return """
00252 %(name)s - MoinMoin daemon
00253 
00254 usage: %(name)s command
00255 
00256 commands:
00257   start     start the server
00258   stop      stop the server
00259   restart   stop then start the server
00260   kill      kill the server
00261 
00262 @copyright: 2004-2005 Thomas Waldmann, Nir Soffer
00263 @license: GNU GPL, see COPYING for details.
""" % {

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.log (   self,
  message 
) [inherited]
TODO: does it work after daemonize? 

Definition at line 217 of file daemon.py.

00217 
00218     def log(self, message):
00219         """ TODO: does it work after daemonize? """
00220         sys.stderr.write(message + '\n')
00221 

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.readPID (   self) [inherited]
Return the pid from the pid file

If there is no pid file, return None. If pid file is corrupted,
remove it. If its not readable, raise.

Definition at line 166 of file daemon.py.

00166 
00167     def readPID(self):
00168         """ Return the pid from the pid file
00169 
00170         If there is no pid file, return None. If pid file is corrupted,
00171         remove it. If its not readable, raise.
00172         """
00173         pid = None
00174         try:
00175             pid = int(file(self.pidFile).read())
00176         except IOError, err:
00177             if err.errno != errno.ENOENT:
00178                 raise
00179         except ValueError:
00180             self.warn("removing corrupted pid file: %s" % self.pidFile)
00181             self.removePID()
00182         return pid

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.removePID (   self) [inherited]

Definition at line 207 of file daemon.py.

00207 
00208     def removePID(self):
00209         try:
00210             os.remove(self.pidFile)
00211         except OSError, err:
00212             if err.errno != errno.ENOENT:
00213                 raise

Here is the caller graph for this function:

Check commandline arguments and run a command 

Definition at line 228 of file daemon.py.

00228 
00229     def run(self):
00230         """ Check commandline arguments and run a command """
00231         args = len(sys.argv)
00232         if args == 1:
00233             self.usage('nothing to do')
00234         elif args > 2:
00235             self.usage("too many arguments")
00236         try:
00237             command = sys.argv[1]
00238             func = getattr(self, self.commandPrefix + command)
00239             func()
00240         except AttributeError:
00241             self.usage('unknown command %r' % command)
00242         except Exception, why:
00243             sys.exit("error: %s" % str(why))

Here is the call graph for this function:

def MoinMoin.util.daemon.Daemon.status (   self) [inherited]
Return status tuple (running, pid)

See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC18

Definition at line 145 of file daemon.py.

00145 
00146     def status(self):
00147         """ Return status tuple (running, pid)
00148 
00149         See http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC18
00150         """
00151         running = False
00152         pid = self.readPID()
00153         if pid is not None:
00154             try:
00155                 os.kill(pid, 0)
00156                 running = True
00157             except OSError, err:
00158                 if err.errno == errno.ESRCH:
00159                     # No such process or security enhancements are causing
00160                     # the system to deny its existence.
00161                     self.log("removing stale pid file: %s" % self.pidFile)
00162                     self.removePID()
00163                 else:
00164                     raise
00165         return running, pid

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.daemon.DaemonScript.usage (   self,
  message 
)

Definition at line 244 of file daemon.py.

00244 
00245     def usage(self, message):
00246         sys.stderr.write('error: %s\n' % message)
00247         sys.stderr.write(self.help())
00248         sys.exit(1)

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.warn (   self,
  message 
) [inherited]

Definition at line 214 of file daemon.py.

00214 
00215     def warn(self, message):
00216         self.log('warning: %s' % message)

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.util.daemon.Daemon.writePID (   self) [inherited]

Definition at line 203 of file daemon.py.

00203 
00204     def writePID(self):
00205         pid = str(os.getpid())
00206         open(self.pidFile, 'wb').write(pid)

Here is the caller graph for this function:


Member Data Documentation

Definition at line 77 of file daemon.py.

string MoinMoin.util.daemon.Daemon.commandPrefix = 'do_' [static, inherited]

Definition at line 63 of file daemon.py.

Definition at line 76 of file daemon.py.

Definition at line 78 of file daemon.py.

Definition at line 75 of file daemon.py.

Definition at line 79 of file daemon.py.


The documentation for this class was generated from the following file: