Posted By

whitetiger on 11/09/06


Tagged

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


Versions (?)


Advertising

Submit Site


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 



Website Promotion
DIRECTORY
is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Expand | Embed | Plain Text
  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.

Download royalty free graphics