We Recommend

Learning Python Learning Python
The authors of Learning Python show you enough essentials of the Python scripting language to enable you to begin solving problems right away, then reveal more powerful aspects of the language one at a time. This approach is sure to appeal to programmers and system administrators who have urgent problems and a preference for learning by semi-guided experimentation.


Posted By

whitetiger on 11/09/06


Tagged

default python c count cli world delicious hello dictionary documentation dangerous daemon fork


Versions (?)


Who likes this?

7 people have marked this snippet as a favorite

px
yuconner
anayhk
adulau
vvarp
vali29
urandom


Python - create daemon


Published in: Python 


  1. def createDaemon():
  2. '''Funzione che crea un demone per eseguire un determinato programma...'''
  3.  
  4. import os
  5.  
  6. # create - fork 1
  7. try:
  8. if os.fork() > 0: os._exit(0) # exit father...
  9. except OSError, error:
  10. print 'fork #1 failed: %d (%s)' % (error.errno, error.strerror)
  11. os._exit(1)
  12.  
  13. # it separates the son from the father
  14. os.chdir('/')
  15. os.setsid()
  16. os.umask(0)
  17.  
  18. # create - fork 2
  19. try:
  20. pid = os.fork()
  21. if pid > 0:
  22. print 'Daemon PID %d' % pid
  23. os._exit(0)
  24. except OSError, error:
  25. print 'fork #2 failed: %d (%s)' % (error.errno, error.strerror)
  26. os._exit(1)
  27.  
  28. funzioneDemo() # function demo
  29.  
  30. def funzioneDemo():
  31.  
  32. import time
  33.  
  34. fd = open('/tmp/demone.log', 'w')
  35. while True:
  36. fd.write(time.ctime()+'\n')
  37. fd.flush()
  38. time.sleep(2)
  39. fd.close()
  40.  
  41. if __name__ == '__main__':
  42.  
  43. createDaemon()

Report this snippet 

You need to login to post a comment.