【问题标题】:How to re-run process Linux after crash? [closed]崩溃后如何重新运行进程Linux? [关闭]
【发布时间】:2017-04-22 04:30:41
【问题描述】:

我有 Python 脚本,它是从 Linux(Cent OS)运行的。

我怎样才能永远运行这个进程(脚本)?还是比如崩溃后会重新运行?

【问题讨论】:

  • 你想从 python 重新启动它吗?
  • 你需要给我们看一些东西。
  • 我有脚本文件:index.py 崩溃后我需要重新运行它

标签: python linux python-2.7 python-3.x centos


【解决方案1】:

1路bash脚本

while true;
do
    python index.py 
done

2路启动python脚本

import os
while True:
    os.system("python index.py")

3路从外部python脚本import index.py然后启动外部脚本。

【讨论】:

    【解决方案2】:

    有一个特殊的变量$? 包含进程返回的值。如果进程正常退出,则为零。您可以在 bash 脚本中使用它来在崩溃后重新运行您的进程。在包含index.py的文件夹中创建文件rerun.sh

    #!/bin/bash    
    t=1
    while [ $t -ne 0]
    do
       python index.py
       t=$?
    done
    

    并使其可执行:

    chmod +x rerun.sh
    

    运行您的 bash 脚本以在崩溃后重新运行您的 python 脚本:

    ./rerun.sh
    

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 1970-01-01
      • 2019-11-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-30
      • 2023-03-29
      • 1970-01-01
      • 2021-10-03
      相关资源
      最近更新 更多