2016-09-03 225 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