1

I new to bash script, and found one of the code from stackoverflow. I merge it with my script, it doesn't run python. When I try to echo, it is always going to "Good". When I tried to run ps -ef | grep runserver* this always came out and causing the python script not running.

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

Here is my code:-

#!/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
NickyMan
  • 39
  • 6

3 Answers3

0

If you are more familiar with python, try this instead:

#!/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 &")
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

Is the following code what you want?

#!/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
njj
  • 11
  • 3
0

@NickyMan, the problem is related to the logic in the shell script. Your program don't find runserver and always says "Good". In this code, if you don't find the server, then 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
Jose Raul Barreras
  • 849
  • 1
  • 13
  • 19