设计一个Shell脚本,能够定期检测系统中运行的进程,并在发现异常进程时进行处理和报警。

Shell脚本示例

#!/bin/bash

# 定义需要监控的进程名
process_name="example_process"

# 检测进程是否运行的函数
check_process() {
  if pgrep "$process_name" > /dev/null
  then
    echo "Process is running"
  else
    echo "Process is not running"
    # 在这里执行异常进程的处理和报警操作,比如发送邮件或记录日志
    # command_to_handle_process
    # command_to_send_alert
  fi
}

# 定期检测进程
while true
  do
    check_process
    sleep 300 # 每5分钟检测一次
  done