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

Linux下如何用语句杀死某个程序运行的所有进程

如何用语句杀死所有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下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进程调度政策


进程调度政策就是调度系统种哪一个进程来CPU运行。这种调度分2层考虑。
第一层,进程状态这个是最优先考虑的,也就是说优先级最高的。在linux中只有就绪态的进程才有可能会被调度选中然后占有CPU,其它状态的进程不可能占有的到CPU。下面是linux中进程的状态
TASK_RUNNING:就绪状态,得到CPU就可以运行。
TASK_INTERRU ......

linux top命令详解

top命令和ps命令的基本作用是相同的,显示系统当前的进程和其它状况;但是top是一个动态显示过程,即可以通过用户按键来不断刷新当前状态。如果在前台执行该命令,它将独占前台,直到用户终止该程序为止。比较准确的说,top命令提供了实时的对系统处理器的状态监视。它将显示系统中CPU最“敏感”的任务列表。该命 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号