2016-12-07 45 views
-1

我有Python腳本,從Linux(Cent OS)運行。如何在崩潰後重新運行進程Linux?

我怎樣才能永遠運行這個過程(腳本)?或者比如它會在崩潰後重新運行?

+1

你想從蟒蛇重新啓動它? –

+0

你需要向我們展示一些東西。 –

+0

我有腳本文件:'index.py'我需要在崩潰後重新運行它 – Griboedov

回答

2

1方式bash腳本

while true; 
do 
    python index.py 
done 

2方式開始Python腳本從外部Python腳本import index.py

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

3方式,然後啓動外部腳本。

1

還有一個特殊變量$?其中包含進程返回的值的。如果進程正常退出,它等於零。您可以在bash腳本中使用它來在崩潰後重新運行您的進程。創建文件rerun.sh的文件夾中包含index.py

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

,並使其可執行:

chmod +x rerun.sh 

運行你的bash腳本崩潰後重新運行您的Python腳本:

./rerun.sh 
相關問題