[转] 使用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使用虚拟存储器技术,这种技术用于提供比计算机系统中实际使用的物理内存大得多的内存 ......
导航:[首页]>[linux内核学习笔记]
目录
[隐藏]
1 字符设备驱动知识讲解
1.1 描述字符设备基本结构体
1.2 作用
1.3 各字段详解
1.4 操作
1.5 实例
1.5.1 代码
1.5.2 运行
[编辑]字符设备驱动知识讲解
作者:[牛涛]
[编辑]描述字符设备基本结构体
/linux/ ......
网上的文章可能过于深入,不太适合新手看,这里介绍最简单的几条SMTP指令,仅需要输入很少的命令即可成功发送一封邮件。
其中粗体部分为输入的命令,蓝色部分为可变内容,灰色为服务器应答内容──
[root@localhost ~]# telnet localhost 25
Trying 127.0.0.1...
Connected to localhost.localdomain (127.0.0.1).
Esc ......
如何用语句杀死所有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 ......
2.4 常用的系统支持
2.4.1 内存申请和释放
include/linux/kernel.h里声明了kmalloc()和kfree()。用于在内核模式下申请和释放内存。
void *kmalloc(unsigned int len,int priority);
void kfree(void *__ptr);
与用户模式下的malloc()不同,kmalloc()申 ......