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

Linux下Python脚本崩溃自动重启实现及Shell脚本报错求助

Fixing Your Auto-Restart Shell Script for Python Programs

Hey there! Let's get that auto-restart script working properly so your Python program stays up and running. First, let's break down the issues in your original script, then walk through a corrected version.

What Was Wrong with the Original Script?

  • Syntax Error: The line if[["$LEN_COMMAND" -le "5"]] is missing critical spaces. In bash, if, [[, and the condition need spaces between them. It should be if [[ "$LEN_COMMAND" -le 5 ]] (also, numbers don't need quotes here).
  • False Positives from grep: When you run ps aux | grep abc, the grep command itself shows up in the process list. This can make your script think the program is running when it's not—we need to exclude the grep process.
  • Unreliable Length Check: Using the length of the ps output to determine if the process is running is shaky. Output length can vary for unexpected reasons, so we should directly check if the process exists instead.

Corrected Shell Script

Here's a robust version of your script that fixes these issues:

#!/usr/bin/env bash
# Check if the target Python process is running
if ! ps aux | grep -E "abc.*def" | grep sudo | grep -v grep > /dev/null; then
    echo "$(date): Restarting the Python program..."
    # Replace this line with your actual program start command
    sudo /usr/bin/python3 /full/path/to/your/program.py &
fi
exit 0

Key Improvements Explained:

  • ! ps aux ... > /dev/null: We redirect output to /dev/null to keep things quiet. The ! reverses the exit status—so if the ps command doesn't find the process, we trigger the restart.
  • grep -E "abc.*def": Uses extended regex to match processes that have both "abc" and "def" in their command line, which is more efficient than chaining multiple grep commands.
  • grep -v grep: Excludes the grep process itself from the results, eliminating false positives.
  • Absolute paths: Using /usr/bin/python3 instead of just python3 ensures Cron uses the correct Python executable (Cron has a limited environment).

Cron Setup Tips

Since Cron's smallest interval is 1 minute, here's how to set up checks every n seconds (example for 10 seconds):
Add these lines to your crontab (run crontab -e to edit):

* * * * * /full/path/to/your/my_prog.sh >> /var/log/program_restart.log 2>&1
* * * * * sleep 10; /full/path/to/your/my_prog.sh >> /var/log/program_restart.log 2>&1
* * * * * sleep 20; /full/path/to/your/my_prog.sh >> /var/log/program_restart.log 2>&1
* * * * * sleep 30; /full/path/to/your/my_prog.sh >> /var/log/program_restart.log 2>&1
* * * * * sleep 40; /full/path/to/your/my_prog.sh >> /var/log/program_restart.log 2>&1
* * * * * sleep 50; /full/path/to/your/my_prog.sh >> /var/log/program_restart.log 2>&1
  • The >> /var/log/program_restart.log 2>&1 part logs all output (and errors) to a file, which is super helpful for debugging.

内容的提问来源于stack exchange,提问作者aja

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 11:37:24