首页 |  我的博客 |  查看该博主内容分类 | 

linux不用三方软件,利用系统服务创建后台守护进程,挂了自动重启

创建服务

vim /etc/systemd/system/自定义name.service

注意:如果第一次打开已经存在内容,说明重名了,换个服务名字

[Unit]
Description=Dingding Bot service  # 自定义描述

[Service]
Type=forking  # 后台运行
ExecStart=/usr/bin/python3 /python-file/app.py   # 具体执行指令
KillMode=process  # 服务停止的同时也会杀死程序主进程
Restart=on-failure  # 系统发生意外导致程序退出时,程序自动重启
RestartSec=3s  # 重启间隔时间

[Install]
WantedBy=multi-user.target

启动

systemctl start 自定义name

  • 有时候会hang住不动,没有输出,正常现象。几秒钟后按ctrl + c退出,服务依然会在后台挂起。如果发现不成功,systemctl status 自定义name查看失败原因。
  • 为了重启后继续运行项目,建议开启自启:
    systemctl enable 自定义name
    • 关闭:systemctl disable 自定义name

重启

systemctl restart 自定义name

停止

systemctl stop 自定义name

posted @ 2023-02-07 17:46  Z哎呀  阅读(527)  评论(0)    收藏  举报