[ת] ʹÓÃPythonдLinuxµÄÊØ»¤½ø³Ì(daemon)
A simple unix/linux daemon in Python
http://www.jejik.com/articles/2007/02/a_simple_unix_linux_daemon_in_python/
by Sander Marechal
I've
written a simple Python class for creating daemons on unix/linux
systems. It was pieced together for various other examples, mostly
corrections to various Python Cookbook
articles and a couple of examples posted to the Python mailing lists.
It has support for a pidfile to keep track of the process. I hope it's
useful to someone.
Below is the Daemon class. To use it, simply subclass it and implement the run() method.
import sys, os, time, atexit
from signal import SIGTERM
class Daemon:
"""
A generic daemon class.
Usage: subclass the Daemon class and override the run() method
"""
def __init__(self, pidfile, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
self.stdin = stdin
self.stdout = stdout
self.stderr = stderr
self.pidfile = pidfile
def daemonize(self):
"""
do the UNIX double-fork magic, see Stevens' "Advanced
Programming in the UNIX Environment" for details (ISBN 0201563177)
http://www.erlenstar.demon.co.uk/unix/faq_2.html#SEC16
"""
try:
pid = os.fork()
if pid > 0:
# exit first parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
os.chdir("/")
os.setsid()
os.umask(0)
# do second fork
try:
pid = os.fork()
if pid > 0:
# exit from second parent
sys.exit(0)
except OSError, e:
sys.stderr.write("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
sys.exit(1)
# redirect standard file descriptors
sys.stdout.flush()
sys.stderr.flush()
si = file(self.stdin, 'r')
so = file(self.stdout, 'a+')
se = file(self.stderr, 'a+', 0)
os.dup2(si.fileno(), sy
Ïà¹ØÎĵµ£º
root@linux ~]# tar [-cxtzjvfpPN] ÎļþÓëĿ¼ ....
²ÎÊý£º
-c £º½¨Á¢Ò»¸öѹËõÎļþµÄ²ÎÊýÖ¸Áî(create µÄÒâ˼)£»
-x £º½â¿ªÒ»¸öѹËõÎļþµÄ²ÎÊýÖ¸Á
-t £º²é¿´ tarfile ÀïÃæµÄÎļþ£¡
Ìرð×¢Ò⣬ÔÚ²ÎÊýµÄÏ´ïÖУ¬ c/x/t ½öÄÜ´æÔÚÒ»¸ö£¡²»¿Éͬʱ´æÔÚ£¡
ÒòΪ²»¿ÉÄÜͬʱѹËõÓë½âѹËõ¡£
-z £ºÊÇ·ñͬʱ¾ßÓÐ gzip µÄÊôÐÔ£¿Ò ......
linuxÈíÖжϻúÖÆ
ÖжϷþÎñ³ÌÐòÍùÍù¶¼ÊÇÔÚCPU¹ØÖжϵÄÌõ¼þÏÂÖ´Ðеģ¬ÒÔ±ÜÃâÖжÏǶÌ׶øʹ¿ØÖƸ´ÔÓ»¯¡£µ«ÊÇCPU¹ØÖжϵÄʱ¼ä²»ÄÜÌ«³¤£¬·ñÔòÈÝÒ׶ªÊ§ÖжÏÐźš£Îª´Ë£¬ Linux½«ÖжϷþÎñ³ÌÐòÒ»·ÖΪ¶þ£¬¸÷³Æ×÷“Top Half”ºÍ“Bottom Half”¡£Ç°Õßͨ³£¶Ôʱ¼äÒªÇó½ÏΪÑϸñ£¬ ......
ÖÐÎÄ°æÊéÄ¿
¡¶Apache CookbookÖÐÎİ棨µÚ¶þ°æ£©¡· New!
¡¶Linux Networking CookbookÖÐÎÄ°æ¡· New!
¡¶Shell½Å±¾Ñ§Ï°Ö¸ÄÏ¡· New!
¡¶×¿ÓгÉЧµÄ³ÌÐòÔ±¡· New!
¡¶´úÂëÖ®ÃÀ¡· New!
¡¶Ç¶ÈëʽӲ¼þÉè¼Æ£¨µÚ¶þ°æ£©¡· New!
¡¶LPI LinuxÈÏ֤ȨÍþÖ¸ÄÏ£¨µÚ¶þ°æ£©¡· New!
¡¶LINUX SERVER HACKS£¨¾í¶þ£©¡· New!
¡¶BSD Hacks¡· ......
¶þ.LinuxϵͳÍøÂçÉ豸Çý¶¯³ÌÐò
¡¡¡¡2.1 ÍøÂçÇý¶¯³ÌÐòµÄ½á¹¹
¡¡¡¡ËùÓеÄLinuxÍøÂçÇý¶¯³ÌÐò×ñÑͨÓõĽӿڡ£Éè¼Æʱ²ÉÓõÄÊÇÃæÏò¶ÔÏóµÄ·½·¨¡£Ò»¸öÉ豸¾ÍÊÇÒ»¸ö¶ÔÏó(device ½á¹¹)£¬ËüÄÚ²¿ÓÐ×Ô¼ºµÄÊý¾ÝºÍ·½·¨¡£Ã¿Ò»¸öÉ豸µÄ·½·¨±»µ÷ÓÃʱµÄµÚÒ»¸ö²ÎÊý¶¼ÊÇÕâ¸öÉ豸¶ÔÏó±¾Éí¡£ÕâÑùÕâ¸ö·½·¨¾Í¿ÉÒÔ´æÈ¡×ÔÉíµÄÊý¾Ý(ÀàËÆÃæÏ ......
¡¡2.4 ³£ÓõÄϵͳ֧³Ö
¡¡¡¡2.4.1 ÄÚ´æÉêÇëºÍÊÍ·Å
¡¡¡¡include/linux/kernel.hÀïÉùÃ÷ÁËkmalloc()ºÍkfree()¡£ÓÃÓÚÔÚÄÚºËģʽÏÂÉêÇëºÍÊÍ·ÅÄÚ´æ¡£
¡¡¡¡void *kmalloc(unsigned int len,int priority);
¡¡¡¡void kfree(void *__ptr);
¡¡¡¡ÓëÓû§Ä£Ê½ÏµÄmalloc()²»Í¬£¬kmalloc()Éê ......