ubuntu服务器监控程序崩溃自动重启

环境:监控程序运行情况分为两种情况,一种带界面,一种控制台程序,带界面程序采用脚本监控方式,不带界面采用Supervisor工具监控。

1. 自动重启带界面程序:

复制代码
#!/bin/sh
while true;
do
processExist=`ps aux | grep 进程的名字 | grep -v "grep" `
if [ -z $processExist ];then
   echo "proecss is restarted"
  /home/hzjj/auto_run.sh  # 启动程序的脚本所在的绝对路径
else
  echo "process is running"
fi
sleep 60 #每 60s检查一次
done

2. 自动重启控制台程序:

安装supervisor:

复制代码
sudo apt update && sudo apt install supervisor

检查其状态:

复制代码
sudo systemctl status supervisor

配置启动文件:

复制代码
sudo touch /etc/supervisor/conf.d/自己名字.conf

文件内容:

复制代码
[program:自己名字-service]
command=python 自己名字.py
autostart=true
autorestart=true
startretries=3
stderr_logfile=/var/log/自己名字.err.log
stdout_logfile=/var/log/自己名字.out.log

测试程序: 自己名字.py

复制代码
import time

def print_with_timestamp(data):
    while True:
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        print(f"[{timestamp}] {data}")
        time.sleep(1)  # 等待一秒

# 示例数据
data_to_print = "这是每秒打印一次的数据"
print_with_timestamp(data_to_print)

启动命令:

复制代码
sudo supervisorctl reread 

sudo supervisorctl update

管理程序命令:

复制代码
sudo supervisorctl

help命令

web界面:

复制代码
sudo vim /etc/supervisor/supervisord.conf

文件内容:
[inet_http_server]
port=*:9001
username=admin
password=admin

参考链接:https://blog.csdn.net/zhumin19910702/article/details/127756095#:\~:text=二、Supervisor安装与配置 1 1.安装 sudo apt update %26%26 sudo,您现在可�%B

相关推荐
梦想很大很大16 小时前
拒绝“盲猜式”调优:在 Go Gin 项目中落地 OpenTelemetry 链路追踪
运维·后端·go
Sinclair17 小时前
内网服务器离线安装 Nginx+PHP+MySQL 的方法
运维
叶落阁主17 小时前
Tailscale 完全指南:从入门到私有 DERP 部署
运维·安全·远程工作
茶杯梦轩1 天前
从零起步学习RabbitMQ || 第二章:RabbitMQ 深入理解概念 Producer、Consumer、Exchange、Queue 与企业实战案例
服务器·后端·消息队列
字节逆旅1 天前
ubuntu应用深度守护
ubuntu
甲鱼9292 天前
MySQL 实战手记:日志管理与主从复制搭建全指南
运维
YuMiao3 天前
gstatic连接问题导致Google Gemini / Studio页面乱码或图标缺失问题
服务器·网络协议
碳基沙盒4 天前
OpenClaw 多 Agent 配置实战指南
运维
Sinclair6 天前
简单几步,安卓手机秒变服务器,安装 CMS 程序
android·服务器
Rockbean7 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek