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 ...
随机推荐
- 使用 requests 访问 HTTPS
当我们访问 HTTPS 的网站时,需要进行证书验证,在浏览器中可以自动处理验证问题,在 Python 中有以下两种做法: import requests //不进行证书验证,但这种方式会出现警告,如下 ...
- AddChild
using UnityEngine; using UnityEngine; using UnityEditor; using System.Collections; public class AddC ...
- ScaleType属性
FIT_CENTER 把原图按照比例放大缩小到ImageView的高度,显示在ImageView的center(中部/居中显示). 1 2 CENTER_CROP 会拉伸图片以原图填满ImageV ...
- CWnd与HWND的区别与转换
CWnd与HWND的区别与转换 2011-10-20 10:29:30| 分类: VC学习库|字号 订阅 一.区别HWND是句柄,CWnd是MFC窗体类,CWnd中包含HWND句柄成员对象是 ...
- 《Lua程序设计》第1章 开始 学习笔记
1.1 程序块(chunk)每段代码(例如一个源代码文件或在交互模式中输入的一行代码),称为一个程序块.若使用命令行参数-i来启动Lua解释器,那么解释器就会在运行完指定程序块后进入交互模式.dofi ...
- RabbitMQ Queue中Arguments属性参数过期队列,过期消息,超时队列的声明
开发十年,就只剩下这套Java开发体系了 >>> 创建队列时指定参数 队列属性:x-message-ttl 可以控制被publish到queue中的message 被丢弃前能够存 ...
- 解决安装laravel/homestead vagrant环境报"A VirtualBox machine with the name 'homestead' already exists."的错误
之前在mac上安装laravel/homestead vagrant虚拟机环境时由于参照的教程是: 每次都必须在~/Homestead目录下边运行vagrant up/halt命令,觉得实在是不方便, ...
- thinkphp5.0 输入变量
可以通过Request对象完成全局输入变量的检测.获取和安全过滤,支持包括$_GET.$_POST.$_REQUEST.$_SERVER.$_SESSION.$_COOKIE.$_ENV等系统变量,以 ...
- Use Reentrant Functions for Safer Signal Handling(译:使用可重入函数进行更安全的信号处理)
Use Reentrant Functions for Safer Signal Handling 使用可重入函数进行更安全的信号处理 How and when to employ reentranc ...
- Android Studio 3.1.2 版本包下载
Android Studio 3.1.2 bug 修复版已发布,本次更新修复了一些错误,并改进了某些场景下 lint 审查的速度.详细的修复内容请查看 Android Studio 3.1.2 的发布 ...