Python - create daemon


/ Published in: Python
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.