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. Weblogic(CVE-2017-10271)漏洞复现

    WebLogic XMLDecoder反序列化漏洞(CVE-2017-10271) 漏洞编号:CVE-2017-10271 漏洞描述:WebLogic WLS组件中存在CVE-2017-10271远程 ...

  2. [Command] sync - 同步内存与硬盘数据

    sync - 同步内存与硬盘之间的数据. sync [--help] [--version] sync 命令将内存中缓存的数据写入磁盘.这包括但不限于修改过的 superblock, inode 和延 ...

  3. iOS使用TFHpple解析html

    iOS 开发中解析html 网上有很多写好的解析框架 今天就来讲一下如何用框架TFHpple来解析html 使用TFHpple解析html github地址:https://github.com/to ...

  4. ldap objectclass

    LDAP中,一个条目必须包含一个objectClass属性,且需要赋予至少一个值.每一个值将用作一条LDAP条目进行数据存储的模板:模板中包含了一个条目必须被赋值的属性和可选的属性.      obj ...

  5. 解决一起web 页面被劫持的案例

    现象 江西客户手机端连接wifi打开URL,页面上显示淘宝店铺广告,使用手机移动网络打开正常,其他地区正常. 二. 处理过程 初步分析:3g.club项目使用了CDN,目前只有江西异常,其他地区无异常 ...

  6. 【转载】C#调用C++ DLL

    using System; using System.Collections.Generic; using System.Linq; using System.Text; //1. 打开项目“Tzb” ...

  7. css3整理--border-image

    border-image语法: border-image : none | <image> [ <number> | <percentage>]{1,4} [ / ...

  8. android make-standalone-toolchain.sh 使用说明

    #$ANDROID_NDK/build/tools/make-standalone-toolchain.sh --platform=android-24 --install-dir=./android ...

  9. java.util.concurrent.Future Basics

    Hereby I am starting a series of articles about future concept in programming languages (also known ...

  10. maven中pom文件中name字段的作用