[转] 使用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
相关文档:
应该说uClinux同标准Linux的最大区别就在于内存管理,同时也由于uClinux的内存管理引发了一些标准Linux所不会出现的问题。本文将把uClinux内存管理同标准Linux的那内存管理部分进行比较分析。
标准Linux使用的虚拟存储器技术
标准Linux使用虚拟存储器技术,这种技术用于提供比计算机系统中实际使用的物理内存大得多的内存 ......
今天在安装oracle过程中,参照网上下载的资料在RHEL5上安装oracle,整个过程比较顺利,但是却遇到了一点问题,虽然不是很严重,但是毕竟是个问题心里还是不很舒服,在此请路过的解决。
主要问题是oracle用户的环境变量,在安装oracle过程中,有一步是需要以oracle用户登录,然后编辑 ......
中文版书目
《Apache Cookbook中文版(第二版)》 New!
《Linux Networking Cookbook中文版》 New!
《Shell脚本学习指南》 New!
《卓有成效的程序员》 New!
《代码之美》 New!
《嵌入式硬件设计(第二版)》 New!
《LPI Linux认证权威指南(第二版)》 New!
《LINUX SERVER HACKS(卷二)》 New!
《BSD Hacks》 ......
三.编写Linux网络驱动程序中需要注意的问题
3.1 中断共享
Linux系统运行几个设备共享同一个中断。需要共享的话,在申请的时候指明共享方式。系统提供的request_irq()调用的定义:
int request_irq(unsigned int irq,
void (*handler)(int irq, void *dev_id, struct pt_regs *re ......