Python开发【笔记】:python程序添加到systemctl系统服务
systemctl系统服务
环境:centos7
实现
正常情况下我们在/usr/lib/systemd/system/目录下,创建一个以.service 后缀的文件,如cdr.service
[Unit]
Description=cdr
After=network.target [Service]
ExecStart=/opt/pbx/cdr/cdr.py
Type=forking [Install]
WantedBy=multi-user.target
使用方式:
# 启动
systemctl start cdr
# 关闭
systemctl stop cdr
#查看状态
systemctl status cdr
#开启自启动
systemctl enable cdr
#关闭开启自启动
systemctl enable cdr
正常的python程序都可以这么用,但是下面这种情况下,还使用上面的.servcie文件创建方式就不好使了,如下
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import logging
import time
import sys logging.basicConfig(level=logging.INFO) def daemon():
import os
# create - fork 1
try:
pid = os.fork()
if pid > 0:
return pid
except OSError as error:
logging.error('fork #1 failed: %d (%s)' % (error.errno, error.strerror))
return -1
# it separates the son from the father
os.chdir('/opt/pbx')
os.setsid()
os.umask(0)
# create - fork 2
try:
pid = os.fork()
if pid > 0:
return pid
except OSError as error:
logging.error('fork #2 failed: %d (%s)' % (error.errno, error.strerror))
return -1
sys.stdout.flush()
sys.stderr.flush()
si = open("/dev/null", 'r')
so = open("/dev/null", 'a+')
se = open("/dev/null", 'a+')
os.dup2(si.fileno(), sys.stdin.fileno())
os.dup2(so.fileno(), sys.stdout.fileno())
os.dup2(se.fileno(), sys.stderr.fileno())
return 0 def main():
pid = daemon()
if pid:
return pid
while True:
logging.info('----------')
time.sleep(1)
main()
这次最大的区别就是在python程序中fork了一个子进程,这种情况下第一种方式就不好使了,经过多次测试发现下面这种方式可以实现我们的效果
[Unit]
Description=lzl
After=network.target [Service]
Type=oneshot
RemainAfterExit=yes
ExecStart=/usr/bin/python3 /home/lzl/workspace/cdrservice/main.py [Install]
WantedBy=multi-user.target
Python开发【笔记】:python程序添加到systemctl系统服务的更多相关文章
- python开发笔记-python调用webservice接口
环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...
- python开发笔记-通过xml快捷获取数据
今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...
- python开发笔记-Python3.7+Django2.2 Docker镜像搭建
目标镜像环境介绍: 操作系统:ubuntu16.04 python版本:python 3.7.4 django版本:2.2 操作步骤: 1. 本地安装docker环境(略)2. 拉取ubunut指定 ...
- python学习笔记-python程序运行
小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...
- 使用Python开发鸿蒙设备程序(0-初体验)
到目前为止,鸿蒙设备开发的"官方指定语言"还是C语言! 这看起来是一件正常的事,毕竟鸿蒙设备开发还是属于嵌入式开发的范畴,而在嵌入式开发中C语言又是当之无愧的首选,所以,大家也都接 ...
- odoo开发笔记--python获取当天时间
取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年 ...
- 【Python开发】python集成开发环境IDE搭建
http://blog.csdn.net/pipisorry/article/details/39854707 使用的系统及软件 Ubuntu / windows Python 2.7 / pytho ...
- Python学习笔记—Python基础1 介绍、发展史、安装、基本语法
第一周学习笔记: 一.Python介绍 1.Python的创始人为吉多·范罗苏姆.1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...
- python开发_IDEL(Python GUI)的使用方法
在这篇blog"Python开发_python的安装"里面你会了解到python的安装. 安装后,我们希望能够运用python GUI来运行一些我们编写的程序,那么Python G ...
随机推荐
- 【代码审计】iCMS_v7.0.7 search.admincp.php页面存在SQL注入漏洞
0x00 环境准备 iCMS官网:https://www.icmsdev.com 网站源码版本:iCMS-v7.0.7 程序源码下载:https://www.icmsdev.com/downloa ...
- 【RF库XML测试】Get Element Text
Name:Get Element TextSource:XML <test library>Arguments:[ source | xpath=. | normalize_whitesp ...
- Linux 查看目录大小及文件数量命令
查看当前目录大小: [root@21andy.com]# du -sh 查看指定目录大小: [root@21andy.com]# du -sh /www/21andy.com 查看当前目录文件总数: ...
- Linux ab 命令
ab 是一个性能测试工具,用来测试一个页面每秒钟能处理多少HTTP请求 [root@localhost ~]$ yum install -y httpd-tools # 安装ab工具 [root@lo ...
- ISD9160学习笔记02_搭建NuMicro开发环境
开发环境这边没什么好说的,烧写玩了玩录音的测试程序. 1. 烧写工具 昨晚先尝试了下烧写工具(NuMicro ICP Programming Tool 1.30.6491.exe),板子自带了烧写器, ...
- React Native(十三)——ios键盘挡住textInput
渐入佳境 用React Native重构的项目也快接近尾声,剩下的就是适配ios的功能了.慢慢地也从中琢磨出了一点门道,于是就遇见了键盘遮挡textInput问题斑斑: 正常页面: android点击 ...
- STL——配接器(adapters)
一.配接器 <Design Patterns>一书提到23个最普及的设计模式,其中对adapter样式的定义如下:将一个class的接口转换为另一个class 的接口,使原本因接口不兼容而 ...
- 《转》Robot Framework 的安装配置和简单的实例介绍
Robot Framework 介绍 Robot Framework 是一款基于 Python 的功能自动化测试框架.它具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进 ...
- VC++进行窗口枚举
https://blog.csdn.net/u012372584/article/details/53735242 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.cs ...
- pg3 bypass源码阅读 —— 学习x64内核hook跳板技术
如之前描述的 pg3复杂了许多 先来看看都要hook哪些点 1.hook dpc和定时器分发器,防止seh路线触发pg KiTimerListExpire,KiRetireDpcList 看一下hoo ...