2016-09-03 280 views
0

我是bash腳本的新手,並且發現了一個來自stackoverflow的代碼。我將它與我的腳本合併,它不會運行python。當我試圖迴應時,它總是會「很好」。當我試圖運行ps -ef | grep runserver*時,它總是出現並導致python腳本無法運行。運行python腳本的Bash腳本

root  1133 0.0 0.4 11988 2112 pts/0 S+ 02:58 0:00 grep --color=auto runserver.py 

這裏是我的代碼: -

#!/bin/sh 
SERVICE='runserver*' 

if ps ax | grep -v grep | grep $SERVICE > /dev/null 
then 
    python /var/www/html/rest/runserver.py 
    python /var/www/html/rest2/runserver.py 
else 
    echo "Good" 
fi 
+0

的答案在這裏http://stackoverflow.com/questions/2903354/bash-script-to-check-running-process – haifzhan

+0

這篇幫助? https://stackoverflow.com/questions/4377109/shell-script-execute-a-python-program-from-within-a-shell-script – Windsooon

+0

@HaifengZhang我用過那個例子,它沒有工作......這就是爲什麼我要求另一個解決方案 – NickyMan

回答

0

如果你比較熟悉的蟒蛇,試試這個來代替:

#!/usr/bin/python 

import os 
import sys 

process = os.popen("ps aux | grep -v grep | grep WHATEVER").read().splitlines() 

if len(process) == 2: 
    print "WHATEVER is running - nothing to do" 
else: 
    os.system("WHATEVER &") 
0

是你想要的下面的代碼?

#!/bin/sh 
SERVER='runserver*' 
CC=`ps ax|grep -v grep|grep "$SERVER"` 
if [ "$CC" = "" ]; then 
    python /var/www/html/rest/runserver.py 
    python /var/www/html/rest2/runserver.py 
else 
    echo "good" 
fi 
0

@NickyMan,問題與shell腳本中的邏輯有關。你的程序沒有找到runserver並總是說「好」。 在這段代碼中,如果你沒有找到服務器,那麼exec runserver

#!/bin/sh 
SERVICE='runserver*' 

if ps ax | grep -v grep | grep $SERVICE > /dev/null 
then 
    echo "Good" 
else 
    python /var/www/html/rest/runserver.py 
    python /var/www/html/rest2/runserver.py 
fi