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

相关推荐
Shota Kishi12 小时前
SLV 新增 allnodes-jito 支持:Solana 验证者多客户端运维与 AI agent 自动化解析
运维·自动化·区块链
闪电悠米13 小时前
黑马点评-Redisson-01_why_redisson
java·服务器·网络·数据库·缓存·wpf
hj28625113 小时前
linux下一步学习内容
linux·运维
睡不醒男孩03082313 小时前
数据库高可用运维实操指南:基于CLup的PostgreSQL生产环境自动化管理
运维·数据库·postgresql
V搜xhliang024614 小时前
临床科研新范式:从选题到投稿,AI智能体如何接管全流程?
运维·数据结构·人工智能·算法·microsoft·数据挖掘·自动化
tudoSearcher14 小时前
日志、指标、链路追踪:可观测性三支柱深度解析
运维·服务器·网络·prometheus
xier_ran14 小时前
【infra之路】Linux基础命令与系统排查
linux·运维·服务器
zh路西法14 小时前
【Linux 串口通信】基于 C++ 多线程的同步/异步串口实现
linux·运维·c++·python
c2385614 小时前
linux基础2
linux·运维·服务器
vortex514 小时前
Linux 默认 SUID 可执行文件详解
linux·运维