systemctl系统服务

  环境:centos7

  systemctl服务使用详解

实现

正常情况下我们在/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系统服务的更多相关文章

  1. python开发笔记-python调用webservice接口

    环境描述: 操作系统版本: root@9deba54adab7:/# uname -a Linux 9deba54adab7 --generic #-Ubuntu SMP Thu Dec :: UTC ...

  2. python开发笔记-通过xml快捷获取数据

    今天在做下python开发笔记之如何通过xml快捷获取数据,下面以调取nltk语料库为例: import nltk nltk.download() showing info https://raw.g ...

  3. python开发笔记-Python3.7+Django2.2 Docker镜像搭建

    目标镜像环境介绍: 操作系统:ubuntu16.04 python版本:python 3.7.4 django版本:2.2 操作步骤: 1.  本地安装docker环境(略)2. 拉取ubunut指定 ...

  4. python学习笔记-python程序运行

    小白初学python,写下自己的一些想法.大神请忽略. 安装python编辑器,并配置环境(见http://www.cnblogs.com/lynn-li/p/5885001.html中 python ...

  5. 使用Python开发鸿蒙设备程序(0-初体验)

    到目前为止,鸿蒙设备开发的"官方指定语言"还是C语言! 这看起来是一件正常的事,毕竟鸿蒙设备开发还是属于嵌入式开发的范畴,而在嵌入式开发中C语言又是当之无愧的首选,所以,大家也都接 ...

  6. odoo开发笔记--python获取当天时间

    取得时间相关的信息的话,要用到python time模块,python time模块里面有很多非常好用的功能,你可以去官方文档了解下,要取的当前时间的话,要取得当前时间的时间戳,时间戳好像是1970年 ...

  7. 【Python开发】python集成开发环境IDE搭建

    http://blog.csdn.net/pipisorry/article/details/39854707 使用的系统及软件 Ubuntu / windows Python 2.7 / pytho ...

  8. Python学习笔记—Python基础1 介绍、发展史、安装、基本语法

    第一周学习笔记: 一.Python介绍      1.Python的创始人为吉多·范罗苏姆.1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言 ...

  9. python开发_IDEL(Python GUI)的使用方法

    在这篇blog"Python开发_python的安装"里面你会了解到python的安装. 安装后,我们希望能够运用python GUI来运行一些我们编写的程序,那么Python G ...

随机推荐

  1. CMake区分MSVC版本

    MSVC++ 4.x _MSC_VER == 1000 MSVC++ 5.0 _MSC_VER == 1100 MSVC++ 6.0 _MSC_VER == 1200 MSVC++ 7.0 _MSC_ ...

  2. 《转》Robot Framework 的安装配置和简单的实例介绍

    Robot Framework 介绍 Robot Framework 是一款基于 Python 的功能自动化测试框架.它具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进 ...

  3. Bootstrap - select2

    1.调整select2下拉框的宽度 <style> .select2-container .select2-choice { height: 28px; line-height: 28px ...

  4. Android设计和开发系列第一篇:Notifications通知(Develop—API Guides)

    Notifications IN THIS DOCUMENT Design Considerations Creating a Notification Required notification c ...

  5. springboot---->springboot中的类型转换(一)

    这里面我们简单的学习一下springboot中关于类型转换器的使用.人世间的事情莫过于此,用一个瞬间来喜欢一样东西,然后用多年的时间来慢慢拷问自己为什么会喜欢这样东西. springboot中的类型转 ...

  6. GitHub 在使用命令行 git push 时报错:The requested URL returned error: 403

    使用 git 的命令行向 GitHub 提交的时候,报错: [Young@localhost OtherLang]$ git push origin master error: The request ...

  7. String 类实现 以及>> <<流插入/流提取运算符重载

    简单版的String类,旨在说明>> <<重载 #include <iostream> //#include <cstring>//包含char*的字符 ...

  8. 【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十:PS/2模块④ — 普通鼠标

    实验十:PS/2模块④ - 普通鼠标 学习PS/2键盘以后,接下来就要学习 PS/2 鼠标.PS/2鼠标相较PS/2键盘,驱动难度稍微高了一点点,因为FPGA(从机)不仅仅是从PS/2鼠标哪里读取数据 ...

  9. Artech的MVC4框架学习——第三章controller的激活

    第一当目标controller的名称通过URL路由被解析出来后,asp.net mvc利用 ControllerBuilder 注册 ControllerFactory ,根据名称实现对目标contr ...

  10. 一、laya学习笔记 --- layabox环境搭建 HelloWorld(坑:ts版本问题解决方案)

    好吧,使用layabox需要从官网下载些啥呢 一.下载layabox 官网 https://www.layabox.com/ 首页上有两个,一个Engine,一个IDE Engine我下载的TS版本, ...