thanks all for sharing, I made it work with the validation on the postgres condition. However (I hate to say this) after hours of googling I'm still not able to perform the loop validation
this is the validation part
Code:
#!/bin/bash
POSTGRES_Status()
{
POSTGRES_PID=`ps -ef | grep postgres | grep data | /bin/awk '{print $2}'`
if [ -z $POSTGRES_PID ]; then
POSTGRES_PID=0
fi
}
POSTGRES_Status
echo "$POSTGRES_PID"
#for i in 1 2 3
#do
POSTGRES_Status
if [ "$POSTGRES_PID" != "0" ]; then
for i in 1 2 3
do
echo "postgres is now running in PID $POSTGRES_PID"
done
else
echo "detected postgres is not running, script will try to start postgres"
sudo -u postgres /opt/msp/pkg/postgres/bin/pg_ctl -D /opt/msp/pkg/postgres/data start
fi
RESULT OF MY TEST SCRIPT ( I intentionally changed the permission of /opt/pgdata/PG_VERSION )
Code:
[ungas@pogi gwapo]$ ./script.sh
0
detected postgres is not running, script will try to start postgres
server starting
[ungas@pogi gwapo]$ 2015-06-30 10:13:23.604 GMT [25759][@] : [1-1]FATAL: could not open file "/opt/pgdata/PG_VERSION": Permission denied
^C - ridiculously stuck on this part I just hit the control C
I'm trying the above part to loop this way
1> check if postgres is up with pid
2> if postgres is up loop will end and the running script as well
3> if postgres is down it will try start
4> if 1st try is not success it will try to turn up again on 2nd time
5> if 2nd try is not success it will try to turn up again on 3rd time
6> if 3rd try fail loop will end and will tell that "start posgres failed to up after three tries" and the script will end