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

相关推荐
lisanmengmeng1 分钟前
nacos nrpe 监控机及被监控机部署
服务器·nacos·nagios·nrpe
贵沫末34 分钟前
Ubuntu——常用软件安装
linux·运维·ubuntu
Doraemomo1 小时前
Linux内核驱动开发——中断与定时器
linux·运维·驱动开发
2301_808414381 小时前
Linux中动静态库的理解
linux·运维·服务器
分布式存储与RustFS2 小时前
模型 checkpoint 放对象存储:分片上传、断点续传与残留清理
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
一条破秋裤3 小时前
Linux 线程创建:pthread_create 与基本回收
java·linux·运维
可涵不会debug3 小时前
第一次学 LangGraph:用一个快递案例搞懂 State、Node、Edge 和 compile
服务器·数据库·mysql
阿钱真强道4 小时前
15 嵌入式操作系统 | 库的下载、编译、安装与验证(libubox / ubus / uci)
ubuntu·cmake·uci·ubus·libubox
Web极客码5 小时前
抹平首包开销:我用 Claude 调优 Nginx 的 Brotli 与 Gzip 动态压缩,平衡 CPU 与网络吞吐
服务器·agent 工作流·agent 图
FED_AF5 小时前
Linux运维“邪修”功法之通配符
linux·运维