后台运行work()方法。
work.py
1 # -*- coding:utf-8 -*- 2 3 def work(): 4 print "running" 5 import time 6 time.sleep(100) 7 8 9 if __name__ == '__main__': 10 work()
方法1 使用nohup命令
nohup python work.py >my.log &
方法2 python-daemon
安装python-daemon
pip install python-daemon
编写入口程序
use_daemon.py
1 # -*- coding:utf-8 -*- 2 3 import daemon 4 from work import work 5 with daemon.DaemonContext(): 6 work()
运行
python use_daemon.py