python to be linux daemon
所需第三方库:python-daemon[https://pypi.python.org/pypi/python-daemon/]
使用方式:
python linux_service.py start/stop/restart
from mythings import start
from daemon import runner
import os
import logging
import inspect class App: def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/foo.pid'
self.pidfile_timeout = 5
self.status = {'alive': True}
this_file = inspect.getfile(inspect.currentframe())
current_path = os.path.abspath(os.path.dirname(this_file))
self.logfile = os.path.join(current_path, 'service.log') def _getLogger(self):
logger = logging.getLogger('[My Service]')
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler(self.logfile)
fh.setLevel(logging.DEBUG)
#ch = logging.StreamHandler()
#ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')
fh.setFormatter(formatter)
#ch.setFormatter(formatter)
logger.addHandler(fh)
#logger.addHandler(ch)
logger.info('init logger...')
return logger def run(self):
self.logger = self._getLogger()
self.logger.info('linux svc do run...')
start(self.status, self.logger) app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()
备注:
日志级别:UNSET < DEBUG < INFO < WARNNING < ERROR<CRITICAL
当ch.setLevel(logging.DEBUG)时,可以打印出级别大于等于DEBUG的日志(包括DEBUG,INFO ,WARNNING , ERROR,CRITICAL)
python to be linux daemon的更多相关文章
- Python下调用Linux的Shell命令
有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...
- python批量修改linux主机密码
+++++++++++++++++++++++++++++++++++++++++++标题:python批量修改Linux服务器密码时间:2019年2月24日内容:基于python实现批量修改linu ...
- python脚本在linux下的执行
假设现有一篇待执行的python脚本test.py python脚本在linux下面执行有两种方式: 打开Linux终端,输入 python test.py 在test.py脚本第一行添加声明 #!/ ...
- [Python]Threading.Thread之Daemon线程
之前对Daemon线程理解有偏差,特记录说明: 一.什么是Daemon A thread can be flagged as a "daemon thread". The sign ...
- [转] Linux Daemon Writing HOWTO
Linux Daemon Writing HOWTO Devin Watson v1.0, May 2004 This document shows how to write a daemon in ...
- 如何在Python中使用Linux epoll
如何在Python中使用Linux epoll 内容 介绍 阻塞套接字编程示例 异步套接字和Linux epoll的好处 epoll的异步套接字编程示例 性能考量 源代码 介绍 从2.6版开始,Pyt ...
- python代码在linux终端中执行报错:Unable to init server: Could not connect: Connection refused
python代码在linux终端中执行时报错: Unable to init server: Could not connect: Connection refused Unable to init ...
- 创建 SysV 风格的 linux daemon 程序
本文介绍如何使用 C 语言创建 Linux 系统中 SysV 风格的 daemon 程序.注意:这是一种旧式的 daemon 程序写法,进入 systemd 时代后是不需要通过这样的方式创建 daem ...
- 小白 Python 爬虫部署 Linux
前言 前面国庆节的时候写过一个简易的爬虫. <Python 简易爬虫实战> 还没看过的同学可以先看一下,这只爬虫主要用来爬取各个博客平台的阅读量等数据,一直以来都是每天晚上我自己手动在本地 ...
随机推荐
- 使用 trash-cli 逃出 rm 命令误删除重要文件的阴影
今天是一个难忘的日子,当时本来想清除我们公司网站cms目录下面一些cdn推送网页后残留的垃圾文件,结果在执行rm -rf conten* 时打成了rm -rf conten *结果就悲剧了.cms目录 ...
- sqlserver权限体系(下)
简介 在上一篇文章中,我对主体的概念做了全面的阐述.本篇文章接着讲述主体所作用的安全对象以及所对应的权限. 理解安全对象(Securable) 安全对象,是SQL Server 数据库引擎授权系统控制 ...
- php打印数组 --- 打印出漂亮格式的数组
htm的<pre>标签,能非常标准的显示数组格式 echo "<pre>";print_r($arr);echo "<pre>&quo ...
- bootstrapValidator.js 做表单验证
有这样的一个场景,我们在提交form表单的时候 可能要做一些验证,比如判断是不是为空,电话的格式验证,邮箱的格式验证等等,手写起来也是可以得. 但是今天我介绍一个bootstrap插件简化开发.就是b ...
- -bash: sudo: command not found Error and Solution
文章转自: http://www.cyberciti.biz/faq/debian-ubuntu-rhel-centos-linux-bash-sudo-command-not-found/ 安装su ...
- 清北暑假模拟day1 艳阳天
/* 注意P有可能不是质数,不要用欧拉函数那一套,正解可以倍增,就是等比数列和的性质,注意n是否为奇数 */ #include <cstdio> #include <algorith ...
- 使用原生JS封装Ajax
使用原生 的JS封装 Ajax,实现 仿JQuery的Ajax,post,get三种异步请求方式: var MAjax = { //根据浏览器创建异步对象 createXhr: function () ...
- POJ 1905 Expanding Rods
Expanding Rods Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 1 ...
- h5交互元素details标签
details是h5新增的交互元素,details与 summary 标签配合使用可以为 details 定义标题.默认情况下,不显示 details 标记中的内容.当用户点击标题时,会显示出 det ...
- hibernate中get,load,list,iterate的用法及比较
首先,get和load都是查询单个对象,而list和iterate为批量查询 注意以下写法仅针对hibernate3的语法. 使用案例如下: // 1. get和load 的用法 Person p = ...