【守护进程】以守护进程运行Python脚本【原创】

概要

目前在做个发布项目,需要一直运行单个Python脚本,以便于对需要发布的任务进行发布逻辑操作,并且是在Linux下运行的

目前能想到的方法有两个:

  • Crontab定时
  • Systemctl守护进程

最后决定使用Systemctl守护进程来做,一方面是因为Crontab最小粒度只支持到一分钟,也就是1分钟执行1次,而这个脚本需要的实时性较高,7、8秒一次,另一方面,使用守护进程来做的话,一旦挂了能实现重启进程


方法

release.py后台常驻运行
以守护进程的方式运行,一旦挂掉自动重启

需要常驻的脚本:release.py

# -*- coding:utf-8 -*-
​
import time
​
​
# 常驻进程,每8秒执行一个任务的发布
while 1:
    print(time.gmtime())
​
    # Todo 执行操作
    run()
​
    # 延时8秒
    time.sleep(8)
常驻进程,每8秒执行一次

编写systemd:
vim /etc/systemd/system/auto_release.service

[Unit]
Description=The python script used for release
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
ExecStart=/usr/bin/python3 /data/zaspace/data/AutoRelease/release.py
Restart=on-failure
[Install]
WantedBy=multi-user.target

启动该脚本并且开机运行
systemctl start auto_release
systemctl enable auto_release

查看该进程的状态

systemctl status auto_release

运行:

● auto_release.service - The python script used for release
   Loaded: loaded (/etc/systemd/system/auto_release.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-08-20 13:46:52 CST; 24h ago
 Main PID: 16652 (python3)
   CGroup: /system.slice/auto_release.service
           └─16652 /usr/bin/python3 /data/zaspace/data/AutoRelease/release.py
​
Aug 21 14:09:03 zabank-toolset-forward.novalocal python3[16652]: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=21, tm_hour=...t=0)
Aug 21 14:09:03 zabank-toolset-forward.novalocal python3[16652]: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=21, tm_hour=...t=0)
Aug 21 14:09:03 zabank-toolset-forward.novalocal python3[16652]: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=21, tm_hour=...t=0)
Hint: Some lines were ellipsized, use -l to show in full.
[root@zabank-toolset-forward AutoRelease]# systemctl status auto_release
● auto_release.service - The python script used for release
   Loaded: loaded (/etc/systemd/system/auto_release.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-08-20 13:46:52 CST; 24h ago
 Main PID: 16652 (python3)
   CGroup: /system.slice/auto_release.service
           └─16652 /usr/bin/python3 /data/zaspace/data/AutoRelease/release.py
​
Aug 21 14:09:03 zabank-toolset-forward.novalocal python3[16652]: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=21, tm_hour=6, tm_min=7, tm_sec=42, tm_wday=2, tm_yday=233, tm_isdst=0)
Aug 21 14:09:03 zabank-toolset-forward.novalocal python3[16652]: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=21, tm_hour=6, tm_min=7, tm_sec=50, tm_wday=2, tm_yday=233, tm_isdst=0)
Aug 21 14:09:03 zabank-toolset-forward.novalocal python3[16652]: time.struct_time(tm_year=2019, tm_mon=8, tm_mday=21, tm_hour=6, tm_min=7, tm_sec=59, tm_wday=2, tm_yday=233, tm_isdst=0)

上面的输出结果含义如下:

  • Loaded行:配置文件的位置,是否设为开机启动
  • Active行:表示正在运行
  • Main PID行:主进程ID
  • Status行:由应用本身(这里是 httpd )提供的软件当前状态
  • CGroup块:应用的所有子进程
  • 日志块:应用的日志

注意:如果该Python脚本有更新的话,需要重启该进程的,不会立即生效的

相关命令

启动该服务 sudo systemctl start xxx.service
启动该服务 sudo systemctl restart xxx.service
停止该服务 sudo systemctl stop xxx.service
查看运行状态 sudo systemctl status xxx.service
设置开机运行 sudo systemctl enable xxx.service

查看是否有启动该Python脚本

ps -ef | grep python

查看日志

journalctl -xu auto_release
发布了64 篇原创文章 · 获赞 47 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/jiandanokok/article/details/100056662