writing a message to the terminal in a daemon process

bharath venkatesh bharathv6.project at gmail.com
Sun Mar 23 16:23:33 EDT 2008


hi,
       i created a daemon process   using the following code

import os
import sys
# Default daemon parameters.
# File mode creation mask of the daemon.
UMASK = 0
# Default working directory for the daemon.
WORKDIR = "/"
# Default maximum for the number of available file descriptors.
MAXFD = 1024
# The standard I/O file descriptors are redirected to /dev/null by default.
if (hasattr(os, "devnull")):
   REDIRECT_TO = os.devnull
else:
   REDIRECT_TO = "/dev/null"
def goDaemon():
   try:
      pid = os.fork()
   except OSError, e:
      raise Exception, "%s [%d]" % (e.strerror, e.errno)
   if (pid == 0):       # The first child.
      os.setsid()
      try:
         pid = os.fork()        # Fork a second child.
      except OSError, e:
         raise Exception, "%s [%d]" % (e.strerror, e.errno)
      if (pid == 0):    # The second child.
         #os.chdir(WORKDIR)
         os.umask(UMASK)
      else:
         os._exit(0)
   else:
      os._exit(0)       # Exit parent of the first child.
   import resource
   maxfd = resource.getrlimit(resource.RLIMIT_NOFILE)[1]
   if (maxfd == resource.RLIM_INFINITY):
      maxfd = MAXFD
   for fd in range(0, maxfd):
      try:
         os.close(fd)
      except OSError:   # ERROR, fd wasn't open to begin with (ignored)
         pass
   os.open(REDIRECT_TO, os.O_RDWR)      # standard input (0)
   os.dup2(0, 1)                        # standard output (1)
   os.dup2(0, 2)                        # standard error (2)
   return(0)

it can be  seen that   the standard output and standard error are redirected
to null terminal now after the  process is made daemon  i.e  after closing
all the resources and redirecting the standard output and standard error to
null terminal now  I want to print some initial msg on the
console(terminal)  in which the program was started .
*NOTE*:I realize that initial msg can be printed before  closing  all the
resources and redirecting the standard output and standard error to null
terminal but it necessary to print the msg  after the process is made daemon
i.e when the process actually starts  doing some processing.

I tried with following code:
fd=os.open("/dev/tty",os.O_WRONLY)
os.write(fd,msg)
os.close(fd)

but this worked fine  before  closing  all the resources and redirecting the
standard output and standard error to null terminal and after doing all
these even it didn't help it didn't print any thing on the terminal

pls can anyone how this can be done
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080324/dbbc5227/attachment.html>


More information about the Python-list mailing list