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

相关推荐
没有梦想的咸鱼185-1037-16633 分钟前
北斗高精度数据解算:破解城市峡谷/长基线/无网区难题,从毫米级定位到自动化交付——(GAMIT/GLOBK底层核心解算技术方法)
运维·arcgis·数据分析·自动化
不怕犯错,就怕不做15 分钟前
linux 如何查看自己的帐号密码及samba的帐号和密码
linux·运维·服务器
实在智能RPA20 分钟前
Agent 在物流行业能实现哪些自动化?——深度拆解 AI Agent 驱动的智慧物流新范式
运维·人工智能·ai·自动化
地下核武21 分钟前
Ubuntu 24.04 在线安装 Qt 6.10.2 后 Qt Creator 无法启动问题记录与解决
linux·qt·ubuntu
张3231 小时前
Linux 启动过程
linux·运维
chinesegf1 小时前
ubuntu建虚拟环境制作docker容器
linux·ubuntu·docker
李彦亮老师(本人)1 小时前
Rocky Linux 9.x 新特性详解
linux·运维·服务器·centos·rocky linux
NiKick1 小时前
在Linux系统上使用nmcli命令配置各种网络(有线、无线、vlan、vxlan、路由、网桥等)
linux·服务器·网络
芳草萋萋鹦鹉洲哦3 小时前
【windows】nginx如何注册为开机自启的服务(WinSW实现)
运维·windows·nginx