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

相关推荐
zzzzzz3106 小时前
9K Star 炸裂开源!这个 C 语言写的代码知识图谱,把 Linux 内核索引压缩到了 3 分钟
linux·服务器·sql
XIAOHEZIcode6 小时前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220701 天前
如何搭建本地yum源(上)
运维
大树884 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠4 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质4 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
小宇宙Zz4 天前
Maven依赖冲突
java·服务器·maven
Inhand陈工4 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
酣大智4 天前
ARP代理--工作原理
运维·网络·arp·arp代理
shushangyun_4 天前
2026年快消品B2B系统推荐:支持终端门店订货、促销政策自动化的工具?
java·运维·网络·数据库·人工智能·spring·自动化