python-daemon实现后台守护进程
- 2019-10-26 18:12:10
- 开发
- 43
- shevechco
我们可以使用很多种方式让python脚本再后台运行,其中最简单的方式是使用linux下的nohup命令
1.nohup命令
nohup python test.py &
这样test.py脚本就脱离了当前终端的控制,我们如果可以使用ps命令查找进程并关闭
2.tmux终端
tmux是一个提供后台终端窗口的会话服务,我们只需要开启tmux,然后再tmux窗口中运行脚本,然后退出tmux即可
3.python-daemon模块
这个模块可以使我们的脚本变为后台守护进程,这个模块需要安装
pip install python-daemon
下面我们有个简单例子
#!/usr/bin/python3 #coding:utf-8 import daemon import time with daemon.DaemonContext(): f = open("/root/r.log", "a+") while True: f.write("test time: {}\n".format(time.time())) f.flush() time.sleep(3)
我们执行python3 test.py运行后,查看r.log文件
tail -f r.log test time: 1572086679.2347727 test time: 1572086682.2360961 test time: 1572086685.2392597 test time: 1572086688.2424757
持续刷新新的记录
内容版权声明:除非注明,否则皆为本站原创文章。
转载注明出处:http://www.sulao.cn/post/731
相关推荐
- python使用pandas模块操作excel
- python获取k8s中使用物理卡pod列表脚本
- python使用toml模块生成containerd仓库配置
- python使用peewee(ORM)操作mysql数据库
- python将浮点数基于754标准的16进制转换
- python操作etcd常用方法
- 容器内执行nvidia-smi报错提示Failed to initialize NVML
- python使用toml模块生成containerd下nvidia-container-runtime配置脚本
- centos7编译安装python3.9
- 评估AI性能的python库ai-benchmark