LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-25-2015, 11:18 AM   #1
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Rep: Reputation: Disabled
started my bash script with issues


Hi Linux Guru's

I'm still not done, but I fully assumed this should work, can you please help me to point what is wrong, I keep getting this message:

[ungas@pogi gwapo]$ ./pogi.sh
Detected /var/tmp/start_host folder is removed, created the folder and start_host.log now
/opt/pgdata is in normal permission values
./pogi.sh: line 45: [: : integer expression expected
Postgres is down
postgres needs to be checked before starting the apps





Code:
#!/bin/bash


# 06424015 - Script composition by Poging gwapo

# pogi home directory
#pogi_HOME=/opt/pogi
# Check the pogi of this box
#POGI=$(ls -r ${pogi_HOME}/pkg | grep jboss | tail -1 | cut -d'-' -f2)
# Check if postgres is running by posting the PID
POSTGRES_PID=$(ps -ef | grep postgres | grep data | /bin/awk '{print $2}')
#echo $POSTGRES_PID
# Check Postgres permission
POSTGRES_FLDR=$(stat -c a /opt/pgdata)


# Check if logging directory is available, if not create one

if [ ! -d '/var/tmp/start_host' ]; then
        mkdir /var/tmp/start_host
        sleep 5
        echo "Detected /var/tmp/start_host folder is removed, created the folder and start_host.log now"
   else
        echo "Verified /var/tmp/start_host exists, created the start_host.log now"
fi



# Check the status of postgres and create a marker in /var/tmp/start_host if the condition is okay


# Check if Postgres folder is in right permission

if [ "$POSTGRES_FLDR" != "700" ] ; then
        echo "Error: /opt/pgdata directory has wrong permissions"
  else
        echo "/opt/pgdata is in normal permission values"
        touch /var/tmp/start_host/pg_folder_ok.txt
#  exit 1
fi


# Check if Postgres is up

if [ "$POSTGRES_PID" -ne "" ]; then
        echo "Postgres up and $POSTGRES_PID PID"
        touch /var/tmp/start_host/pg_up.txt
  else
        echo "Postgres is down"
fi

# Create a counter, if the count is equal to 2 then apps should start

        L=0
        [ -e /var/tmp/start_host/pg_folder_ok.txt ] && (( L++ ))
        [ -e /var/tmp/start_host/pg_up.txt ] && (( L++ ))

                if [ L == 2 ];
                        then
                                echo "apps are good to start now"
                        else
                                echo "postgres needs to be checked before starting the apps"
                fi
the error seems to be on this part, but as far as I know this is okay and the script does not create pg_up.txt

Code:
if [ "$POSTGRES_PID" -ne "" ]; then
        echo "Postgres up and $POSTGRES_PID PID"
        touch /var/tmp/start_host/pg_up.txt
  else
        echo "Postgres is down"
fi

Last edited by apss_evaluator; 06-25-2015 at 11:51 AM.
 
Old 06-25-2015, 11:47 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,830

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
insert set -xv at the beginning to see what's happening.
I assume the in line 20 the returned value was not a number, but I don't know what's really happened.
 
1 members found this post helpful.
Old 06-25-2015, 11:50 AM   #3
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
insert set -xv at the beginning to see what's happening.
I assume the in line 20 the returned value was not a number, but I don't know what's really happened.
Hi Pan64,
thanks for your reply, when you say "set -xv" was that run the script and add that as a parameter?
 
Old 06-25-2015, 12:33 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Put set -xv as the second line of the script (the first being the shebang)
 
1 members found this post helpful.
Old 06-25-2015, 12:42 PM   #5
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Code:
if [ "$POSTGRES_PID" -ne "" ]; then
-ne is a numeric comparison. You can't use an empty string as an argument here.

works:
Code:
if [ "$POSTGRES_PID" != "" ]; then
better:
Code:
if [ -n "$POSTGRES_PID" ]; then
even better:

Code:
if [[ -n "$POSTGRES_PID" ]]; then
 
1 members found this post helpful.
Old 06-25-2015, 06:26 PM   #6
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Put set -xv as the second line of the script (the first being the shebang)
thanks
 
Old 06-25-2015, 06:27 PM   #7
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by millgates View Post
Code:
if [ "$POSTGRES_PID" -ne "" ]; then
-ne is a numeric comparison. You can't use an empty string as an argument here.

works:
Code:
if [ "$POSTGRES_PID" != "" ]; then
better:
Code:
if [ -n "$POSTGRES_PID" ]; then
even better:

Code:
if [[ -n "$POSTGRES_PID" ]]; then
thanks millgates, I followed "even better" and issue resolved
 
Old 06-30-2015, 05:21 AM   #8
apss_evaluator
Member
 
Registered: Mar 2012
Posts: 115

Original Poster
Rep: Reputation: Disabled
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

Last edited by apss_evaluator; 06-30-2015 at 05:41 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Terminating YouTube html video started with firefox in bash script julianvb Linux - Software 1 09-29-2014 02:54 PM
[SOLVED] bash script runs perfectly when started manually - but not when started by CRON jonasbdk Linux - General 12 08-27-2014 05:54 AM
[SOLVED] bash script how kill app running in bg that was started by the script porphyry5 Programming 2 07-26-2014 01:18 PM
[SOLVED] bash and xterm: how make apps started by and for a script persist when script terminates porphyry5 Linux - General 4 06-15-2011 01:27 PM
bash script works when interactive, endless loop when started via cron dguy Linux - General 5 04-10-2006 11:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration