You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Linux CentOS上实现MongoDB进程终止后自动启动

Got it, let's break down how to solve this problem—you want MongoDB to automatically restart if it gets killed (like with Ctrl+C), and also understand how to monitor its shutdown events to trigger a restart. Here are a few solid, tested approaches:

方案1:利用Systemd实现自动重启(推荐)

This is the most reliable method for Linux systems, since systemd is built to monitor and manage service processes. It'll automatically restart MongoDB no matter why it stops—whether it's a manual Ctrl+C, crash, or normal exit.

  1. Locate MongoDB's systemd service file. Common paths are /etc/systemd/system/mongodb.service or /lib/systemd/system/mongodb.service.
  2. Edit the file and find the [Service] section. Add or modify these parameters:
    Restart=always
    RestartSec=5  # Wait 5 seconds before restarting, adjust as needed
    
    • Restart=always means systemd will restart MongoDB regardless of how it terminates. If you only want restarts on crashes, use Restart=on-failure instead.
  3. Save the file, reload systemd's configuration, and restart the service:
    sudo systemctl daemon-reload
    sudo systemctl restart mongodb
    
  4. Test it out: Kill MongoDB with sudo systemctl stop mongodb or Ctrl+C, then check status with sudo systemctl status mongodb after a few seconds—it should be back up.

方案2:自定义监控脚本

If you're on a system without systemd (like older Linux distros, macOS, or Windows), a simple polling script will work. It checks if MongoDB is running at regular intervals and restarts it if not.

Linux/macOS Bash Script

#!/bin/bash

# Replace these paths with your actual MongoDB command and config
MONGODB_START="/usr/bin/mongod --config /etc/mongod.conf"
PROCESS_NAME="mongod"

while true; do
    # Check if mongod process is active
    if ! pgrep -x "$PROCESS_NAME" > /dev/null; then
        echo "$(date): MongoDB process missing—restarting..."
        $MONGODB_START &
    fi
    sleep 10  # Check every 10 seconds, tweak this value
done
  • Save as mongodb_monitor.sh, make it executable: chmod +x mongodb_monitor.sh
  • Run it in the background: nohup ./mongodb_monitor.sh > mongodb_monitor.log 2>&1 &

Windows Batch Script

@echo off
set MONGODB_EXE="C:\Program Files\MongoDB\Server\6.0\bin\mongod.exe"
set CONFIG_FILE="C:\Program Files\MongoDB\Server\6.0\bin\mongod.cfg"

:loop
:: Check if mongod is running
tasklist /FI "IMAGENAME eq mongod.exe" 2>NUL | find /I /N "mongod.exe">NUL
if "%ERRORLEVEL%"=="1" (
    echo %date% %time%: MongoDB not running—restarting...
    start "" %MONGODB_EXE% --config %CONFIG_FILE%
)
timeout /t 10 /nobreak > NUL
goto loop

监听MongoDB Shutdown Events

If you want to trigger a restart exactly when MongoDB shuts down (instead of polling), you can monitor its log file for shutdown-related messages.

  1. Find your MongoDB log path (check the systemLog.path setting in your mongod.conf—common path: /var/log/mongodb/mongod.log).
  2. Use this script to listen for shutdown signals in real time:
#!/bin/bash

MONGODB_START="/usr/bin/mongod --config /etc/mongod.conf"
LOG_FILE="/var/log/mongodb/mongod.log"

# Tail the log and watch for shutdown keywords
tail -F "$LOG_FILE" | while read line; do
    # Match common shutdown messages (adjust for your MongoDB version if needed)
    if echo "$line" | grep -E "(Shutting down|Received signal 2|Terminating)"; then
        echo "$(date): Detected MongoDB shutdown—restarting..."
        $MONGODB_START &
    fi
done
  • Received signal 2 is the log entry for a Ctrl+C (SIGINT) shutdown
  • Shutting down appears during normal termination
  • This script will immediately trigger a restart as soon as it sees these messages

内容的提问来源于stack exchange,提问作者Ralph Marvin Addo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:34:24