1
2
3 import sys, os, time
4
5 def main():
6 f = open("/tmp/hello-world", "w")
7 while 1:
8 f.write('%s\n' % time.ctime(time.time()))
9 f.flush()
10 time.sleep(10)
11
12
13 if __name__ == "__main__":
14 try:
15 pid = os.fork()
16 if pid > 0:
17
18 sys.exit(0)
19 except OSError, e:
20 print >>sys.stderr, "fork #1 failed: %d (%s)" % (e.errno, e.strerror)
21 sys.exit(1)
22
23 os.chdir("/")
24 os.setsid()
25 os.umask(0)
26
27 try:
28 pid = os.fork()
29 if pid > 0:
30 print "Daemon PID %d" % pid
31 sys.exit(0)
32 except OSError, e:
33 print >>sys.stderr, "fork #2 failed: %d (%s)" % (e.errno, e.strerror)
34 sys.exit(1)
35
36 main()
Python/HardCoreBeispiel (zuletzt geändert am 2007-12-23 22:47:31 durch localhost)