易截截图软件、单文件、免安装、纯绿色、仅160KB

[转] 使用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


相关文档:

linux软中断机制


 
 
linux软中断机制
 
 
中断服务程序往往都是在CPU关中断的条件下执行的,以避免中断嵌套而使控制复杂化。但是CPU关中断的时间不能太长,否则容易丢失中断信号。为此, Linux将中断服务程序一分为二,各称作“Top Half”和“Bottom Half”。前者通常对时间要求较为严格, ......

Linux学习线路...


中文版书目
《Apache Cookbook中文版(第二版)》 New!
《Linux Networking Cookbook中文版》 New!
《Shell脚本学习指南》 New!
《卓有成效的程序员》 New!
《代码之美》 New!
《嵌入式硬件设计(第二版)》 New!
《LPI Linux认证权威指南(第二版)》 New!
《LINUX SERVER HACKS(卷二)》 New!
《BSD Hacks》 ......

linux下tomcat自启动

-----------------------------------------------------------
#!/bin/bash
#
# Startup script for the tomcat
#
# chkconfig: 345 95 15
# description: tomcat service script
#
# Source function library.
. /etc/rc.d/init.d/functions
TOMCAT_HOME=/home/tomcat
RETVAL=0
checkjava(){
if [ -z "$JAVA ......

Linux网络驱动程序编写(三)

 2.4 常用的系统支持
  2.4.1 内存申请和释放
  include/linux/kernel.h里声明了kmalloc()和kfree()。用于在内核模式下申请和释放内存。
  void *kmalloc(unsigned int len,int priority);
  void kfree(void *__ptr);
  与用户模式下的malloc()不同,kmalloc()申 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号