[ת] ʹÓÃ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
Ïà¹ØÎĵµ£º
½üÈÕ£¬ÔÚLinuxCON 2009´ó»áÉÏ£¬×ÀÃæ Linux ÓÖ³ÉΪҵ½ç¹Ø×¢µÄ½¹µãÖ®Ò»¡£¸÷ÖÖ²»Í¬µÄÉùÒô£¬É¤ÃŶ¼ºÜ´ó¡£ÔÚÕâ·×·×ÈÂȵķÕΧÖУ¬ Linux ÈçºÎ×ß³ö×ÀÃæÀ§¾³£¿
×ݹÛÈ«¾Ö£¬ÆÕ¼°×ÀÃæ Linux µÄ×î´óÕÏ°Ö®Ò»ÊÇϵͳ°²×°µÄ²Ù×÷À§ÄÑÐÔ¡£Ô¤×° Windows Òѳɴó ......
ÎÒÃÇ˵һÏÂLinuxÖеÄinitÃüÁî
InitÊÇLinux²Ù×÷ϵͳÖв»¿ÉȱÉٵijÌÐòÖ®Ò»¡£init½ø³ÌÊÇLinuxÄÚºËÒýµ¼ÔËÐеģ¬ÊÇϵͳÖеĵÚÒ»¸ö½ø³Ì£¬Æä½ø³ÌºÅ£¨PID£©ÓÀԶΪ1¡£Äã¿ÉÒÔͨ¹ý#ps -ef|headÀ´²é¿´½ø³ÌÃüÁî¡£
1) ¼¸¸ö³£ÓõÄÃüÁî
²é¿´ÏµÍ³½ø³ÌÃüÁ#ps -ef|head
&n ......
ÖÐÎÄ°æÊéÄ¿
¡¶Apache CookbookÖÐÎİ棨µÚ¶þ°æ£©¡· New!
¡¶Linux Networking CookbookÖÐÎÄ°æ¡· New!
¡¶Shell½Å±¾Ñ§Ï°Ö¸ÄÏ¡· New!
¡¶×¿ÓгÉЧµÄ³ÌÐòÔ±¡· New!
¡¶´úÂëÖ®ÃÀ¡· New!
¡¶Ç¶ÈëʽӲ¼þÉè¼Æ£¨µÚ¶þ°æ£©¡· New!
¡¶LPI LinuxÈÏ֤ȨÍþÖ¸ÄÏ£¨µÚ¶þ°æ£©¡· New!
¡¶LINUX SERVER HACKS£¨¾í¶þ£©¡· New!
¡¶BSD Hacks¡· ......
ÈçºÎÓÃÓï¾äɱËÀËùÓÐoracle´ø(LOCAL=NO)µÄ½ø³Ì£¿
·½·¨Ò»£º(½øÈëoracleÓû§ÏÂ)
$ a=`ps -ef |grep oracle$ORACLE_SID|grep LOCAL=NO |awk '{print $2}'`
»òÕß (È¥³ýµ±Ç°grep½ø³Ì)
$ a=`ps -ef |grep oracle$ORACLE_SID|grep LOCAL=NO |grep -v grep|awk '{print $2}'`
$ echo $a
$ kill -9 $a
·½·¨¶þ£º£¨Ö±½Óɱ£©
$ p ......
ÐòÑÔ
¡¡¡¡LinuxÊÇUnix²Ù×÷ϵͳµÄÒ»ÖÖ±äÖÖ£¬ÔÚLinuxϱàдÇý¶¯³ÌÐòµÄÔÀíºÍ˼ÏëÍêÈ«ÀàËÆÓÚÆäËûµÄUnixϵͳ£¬µ«Ëüdos»òwindow»·¾³ÏµÄÇý¶¯³ÌÐòÓкܴóµÄÇø±ð¡£ÔÚLinux»·¾³ÏÂÉè¼ÆÇý¶¯³ÌÐò£¬Ë¼Ïë¼ò½à£¬²Ù×÷·½±ã£¬¹¦ÄÜÒ²ºÜÇ¿´ó£¬µ«ÊÇÖ§³Öº¯ÊýÉÙ£¬Ö»ÄÜÒÀÀµkernelÖеĺ¯Êý£¬ÓÐЩ³£ÓõIJÙ×÷Òª×Ô¼ºÀ´±àд£¬¶øÇÒµ÷ÊÔÒ²²»·½±ã¡£±¾ÈËÕâ¼ ......