2016-09-03 17 views
0

私はbashスクリプトを初めて使用しましたが、stackoverflowのコードの1つが見つかりました。私はスクリプトとマージしますが、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

あなたのpythonに慣れている場合は、代わりにこれを試してみてください。

#!/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

@ニッキーマン、この問題は、シェルスクリプトのロジックに関連しています。あなたのプログラムは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 
関連する問題