所需第三方库: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的更多相关文章

  1. Python下调用Linux的Shell命令

    有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...

  2. python批量修改linux主机密码

    +++++++++++++++++++++++++++++++++++++++++++标题:python批量修改Linux服务器密码时间:2019年2月24日内容:基于python实现批量修改linu ...

  3. python脚本在linux下的执行

    假设现有一篇待执行的python脚本test.py python脚本在linux下面执行有两种方式: 打开Linux终端,输入 python test.py 在test.py脚本第一行添加声明 #!/ ...

  4. [Python]Threading.Thread之Daemon线程

    之前对Daemon线程理解有偏差,特记录说明: 一.什么是Daemon A thread can be flagged as a "daemon thread". The sign ...

  5. [转] Linux Daemon Writing HOWTO

    Linux Daemon Writing HOWTO Devin Watson v1.0, May 2004 This document shows how to write a daemon in ...

  6. 如何在Python中使用Linux epoll

    如何在Python中使用Linux epoll 内容 介绍 阻塞套接字编程示例 异步套接字和Linux epoll的好处 epoll的异步套接字编程示例 性能考量 源代码 介绍 从2.6版开始,Pyt ...

  7. 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 ...

  8. 创建 SysV 风格的 linux daemon 程序

    本文介绍如何使用 C 语言创建 Linux 系统中 SysV 风格的 daemon 程序.注意:这是一种旧式的 daemon 程序写法,进入 systemd 时代后是不需要通过这样的方式创建 daem ...

  9. 小白 Python 爬虫部署 Linux

    前言 前面国庆节的时候写过一个简易的爬虫. <Python 简易爬虫实战> 还没看过的同学可以先看一下,这只爬虫主要用来爬取各个博客平台的阅读量等数据,一直以来都是每天晚上我自己手动在本地 ...

随机推荐

  1. HBase命令(一) -- 库操作

    打开数据库 bin/start-hbase.sh //打开HBase bin/hbase shell //以命令行的方式打开Hbase控制台 Rest接口开启 bin/hbase rest //普通的 ...

  2. Double和double的区别

    1.Double是java定义的类,而double是预定义数据类型(8种中的一种)2.Double就好比是对double类型的封装,内置很多方法可以实现String到double的转换,以及获取各种d ...

  3. Nginx for Windows 使用笔记

    一.常见启动错误 1. "No mapping for the Unicode character exists in the target multi-byte code page&quo ...

  4. XStream使用总结

    最近做webService报文转换的公共接口使用到了XSream工具库,写个小总结备忘... XStream是一个可以将javaBean与XML双向转换的java类库,本文内容基于xstream-1. ...

  5. JS中document对象和window对象有什么区别

    简单来说,document是window的一个对象属性.Window 对象表示浏览器中打开的窗口.如果文档包含框架(frame 或 iframe 标签),浏览器会为 HTML 文档创建一个 windo ...

  6. 2015年12月12 Node.js实战(一)使用Express+MongoDB搭建多人博客

    序,Node是基于V8引擎的服务器端脚本语言. 基础准备 Node.js: Express:本文用的是3.21.2版本,目前最新版本为4.13.3,Express4和Express3还是有较大区别,可 ...

  7. Linux下安装和配置JDK与Tomcat(入门版)

    JDK路径:/usr/java/jdk1.6.0_25 Tomcat路径:/usr/local/apache-tomcat 1. 下载jdk6.0(选择“.rpm.bin”结尾的,6u25版本) ht ...

  8. mysql python image

    连接mysql数据库: cnx = mysql.connector.connect(user='joe', database='test') Connector/Python参数列表 Argument ...

  9. css固定元素位置(fixed)

    来源:http://www.cnblogs.com/lecaf/archive/2011/03/25/fixed.html fixed是一种特殊的absolute,同样不占文档流,特殊的地方在于fix ...

  10. Spring 事务知识

    1.1  Spring注解的各种行为 事物传播注解: @Transactional(propagation=Propagation.REQUIRED) (常用) 如果有事务, 那么加入事务, 没有的话 ...