python脚本后台运行

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

问题描述:

环境: CentOS6.4

一个用python写的监控脚本test1.py,用while True方式一直运行,在ssh远程(使用putty终端)时通过以下命令启动脚本:

python test1.py &

现在脚本正常运行,通过ps能看到进程号,此时直接关闭ssh终端(不是用exit命令,是直接通过putty的关闭按钮执行的), 再次登录后发现进程已经退出了。

通过后台启动的方式该问题已经解决,这里总结下,也方便我以后查阅。

linux下后台运行通过fork实现linux环境下,在c中守护进程是通过fork方式实现的,python也可以通过该方式实现,示例代码如下:

1#!/usr/bin/env python2#E-Mail : Mike_Zhang@live.com3importtime,platform4importos56deffunzioneDemo():7#这是具体业务函数示例8fout = open('/tmp/demone.log','w')9whileTrue:10fout.write(time.ctime()+'\n')11fout.flush()12time.sleep(2)13fout.close()1415defcreateDaemon():16#fork进程17try:18ifos.fork() >0: os._exit(0)19exceptOSError, error:20PRint'fork #1 failed: %d (%s)'%(error.errno, error.strerror)21os._exit(1)22os.chdir('/')23os.setsid()24os.umask(0)25try:26pid =os.fork()27ifpid >0:28print'Daemon PID %d'%pid29os._exit(0)30exceptOSError, error:31print'fork #2 failed: %d (%s)'%(error.errno, error.strerror)32os._exit(1)33#重定向标准IO34sys.stdout.flush()35sys.stderr.flush()36si = file("/dev/null",'r')37so = file("/dev/null",'a+')38se = file("/dev/null",'a+', 0)39os.dup2(si.fileno(), sys.stdin.fileno())40os.dup2(so.fileno(), sys.stdout.fileno())41os.dup2(se.fileno(), sys.stderr.fileno())4243#在子进程中执行代码44funzioneDemo()#function demo4546if__name__=='__main__':47ifplatform.system() =="Linux":48createDaemon()49else:50os._exit(0)

通过upstart方式实现

可以通过upstart把应用封装成系统服务,这里直接记录下完整示例。

1、编写python脚本

[root@local t27]# cat test123.py

#!/usr/bin/env python

import os,time

while True :

print time.time()

time.sleep(1)

2、编写upstat配置文件

[root@local t27]# cat /etc/init/mikeTest.conf

description "My test"

author "Mike_Zhang@live.com"

start on runlevel [234]

stop on runlevel [0156]

chdir /test/t27

exec /test/t27/test123.py

respawn

3、重新加载upstate

initctl reload-configuration

4、启动服务

[root@local t27]# start mikeTest

mikeTest start/running, process 6635

[root@local t27]# ps aux | grep test123.py

root 6635 0.0 0.0 22448 3716 ? Ss 09:55 0:00 python /test/t27/test123.py

root 6677 0.0 0.0 103212 752 pts/1 S+ 09:56 0:00 grep test123.py

5、停止服务

[root@local t27]# stop mikeTest

mikeTest stop/waiting

[root@local t27]# ps aux | grep test123.py

root 6696 0.0 0.0 103212 752 pts/1 S+ 09:56 0:00 grep test123.py

[root@local t27]#

通过bash脚本实现1、python代码

[root@local test]# cat test123.py

#!/usr/bin/env python

import os,time

while True :

print time.time()

time.sleep(1)

2、编写启动脚本

[root@local test]# cat start.sh

#! /bin/sh

python test123.py &

3、启动进程

[root@local test]#./start.sh

如果直接用&启动进程:

python test123.py &

直接关闭ssh终端会导致进程退出。

通过screen、tmux等方式实现如果临时跑程序的话,可以通过screen、tmux启动程序,这里描述下tmux启动的方式。

1、启动tmux

在终端输入tmux即可启动

2、在tmux中启动程序

直接执行如下命令即可(脚本参考上面的): python test123.py

3、直接关闭ssh终端(比如putty上的关闭按钮);

4、重新ssh上去之后,执行如下命令:

tmux attach

现在可以看到python程序还在正常执行。

windows下后台运行在windows下没有深入的研究过,我经常用的方法是修改python脚本的扩展名为".pyw",双击即可后台运行,不需要修改任何代码。

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航