易截截图软件、单文件、免安装、纯绿色、仅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


相关文档:

Windows程序员如何转向Linux开发应用?

Windows程序员如何转向Linux开发应用?
这是一封发到邮箱里面的邮件,感觉有点代表性,这里做个统一回答,一家之言哈,欢迎拍砖。
原文如下:
我从csdn学习大本营得到您的信息。不好意思打搅您。
我现在用c++在linux下开发大型应用程序。我想请教是否值得深入学习linux kernel。
我没有特别多的时间。另外我有多年Wind ......

借助sniffer诊断Linux网络故障


嗅探器(sniffer)在网络安全领域是一把双刃剑,一方面常被黑客作为网络攻击工具,从
而造成密码被盗、敏感数据被窃等安全事件;另一方面又在协助网络管理员监测网络状况、诊断网络故障、排除网络隐患等方面有着不可替代的作用。嗅探器是企业
必不可少的网络管理工具。本文以Linux平台下三个常用的网络嗅探器Tcpdump、Eth ......

Linux学习线路...


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

Linux 文件系统概述(Linux Sir)

Linux 文件系统概述

作者:北南南北
来自:LinuxSir.Org
摘要: 本文通过文件系统的定义说起,然后通过引文简单的介绍了一下文件系统类型;对Linux常用的ext2、ext3及reiserfs 根据本人使用经验也泛泛的谈了谈,但并不是专业的。如何阅读本文,还是用马克思理论告诉我们的方法:一分为二,边看边批吧;
目录索引
一 ......

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

 三.编写Linux网络驱动程序中需要注意的问题
  3.1 中断共享
  Linux系统运行几个设备共享同一个中断。需要共享的话,在申请的时候指明共享方式。系统提供的request_irq()调用的定义:
    int request_irq(unsigned int irq,
          void (*handler)(int irq, void *dev_id, struct pt_regs *re ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号