所需第三方库: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. sqlmap写文件为空之谜

    恰逢有一个SQL注入可以通过sqlmap进行,而且权限高得离谱,直接就是root权限.既然是root权限当然是想直接getshell咯.可是只是sqlmap -u xxx --os-shell的时候却 ...

  2. 使用Lucene开发自己的搜索引擎

    1.下载Lucene开发包,请到:http://lucene.apache.org/ 2.在myeclipse环境部署该开发包: 3.代码编写: package Lucene; import java ...

  3. Yii2.0 实现的短信发送

    原文地址:http://www.phpxs.com/post/4245/ yii2-smserGithub项目主页 https://github.com/daixianceng/yii2-smser ...

  4. php 数组排序以及按照某个字段排序

    经常,开发人员发现在PHP中使用这种数据结构对值或者数组元素进行排序非常有用.PHP提供了一些适合多种数组的排序函数,这些函数允许你在数组内部对元素进行排列,也允许用很多不同的方法对它们进行重新排序. ...

  5. yaf扩展

  6. shell学习之路:流程控制(while)

    while循环: 介绍:while循环是不定循环,也称作条件循环.只要条件判断成立,循环就会一直继续执行,直到条件判断不成立,循环才会停止,这就是和for的固定循环不太一样了. while [ 条件判 ...

  7. 再读<<基于MVC的JavaScript Web 富应用开发>>

    工作的时候粗读过这本书的几章内容,真真是囫囵吞枣~~目前手边就剩这一本,重新读才觉得先前是没看明白啊!这个作者博闻强识,对这些插件.库了解的非常多.记录下,查的资料 订阅/发布 jQuery Tiny ...

  8. 利用flexbox实现按字符长度排列dom元素

    说明:请使用chrome浏览器打开 See the Pen pvyjGV by lilyH (@lilyH) on CodePen. 如上图所示,我们你要实现的效果就是,(1)在一行中显示两块元素:( ...

  9. PHP基础之 string 字符串函数

    /*=================常用字符串处理函数================== ltrim();        //去掉字符串左边的空格 rtrim();         //去掉字符串 ...

  10. PHP 暂停函数 sleep() 与 usleep() 的区别

    在PHP中暂停代码执行一定时间,有两个函数可以实现,一个是sleep(),另一个是usleep(),它们参数都是一个整数值.sleep()是暂停多少秒,usleep()是暂停多少微秒. 注意:usle ...