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

  • A+
所属分类:其他杂项
本文信息本文由方法SEO顾问发表于2024-07-1710:40:21,共 2851 字,转载请注明:【守护进程】以守护进程运行Python脚本_【方法SEO顾问】

概要

目前在做个发布项目,需要一直运行单个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秒执行一次123456789101112131415

编写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.target123456789

启动该脚本并且开机运行

systemctl start auto_release
systemctl enable auto_release12

查看该进程的状态

systemctl status auto_release1

运行:

● 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 bank-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 bank-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 bank-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@bank-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 bank-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 bank-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 bank-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)12345678910111213141516171819202122

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

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

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

相关命令

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

查看是否有启动该Python脚本

ps -ef | grep python1

查看日志

journalctl -xu auto_release1

  • 版权声明:除非注明,本博客均为北京SEO方法的原创文章,转载或引用请以超链接形式标明本文地址,否则会在SEO圈内公开此种不尊重版权的行为,谢谢合作!本文地址:https://seofangfa.com/other-note/python_jiandanokok.html
  • 转载请注明:【守护进程】以守护进程运行Python脚本_ 【方法SEO顾问】

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: