LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Re-evaluate Variable in script (https://www.linuxquestions.org/questions/linux-newbie-8/re-evaluate-variable-in-script-4175621289/)

oimoit 01-09-2018 11:54 AM

Re-evaluate Variable in script
 
Hi

I declared a variable in the beginning of my script.

This variable basically executes a command that will find the PID of a process. (line 7 of script)

Then I stop the application. (line 20 of script)

I restart it (line 23 of script) and obviously, it is getting a new PID. (line 27 I execute the same command from the variable to get the new PID and it works)

Output shows that the variable is getting the PID correctly before the restart:
Code:

#PID only
PID_EXT_PURGE=`ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep|awk '{ print $2 }'`
ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep|awk '{ print $2 }'
++ ps -fu sbi
++ grep ExternalPurgeConsole.sh
++ grep -v grep
++ awk '{ print $2 }'
+ PID_EXT_PURGE=34209998

however, I am not able to re-use the variable once the application is restarted to get the new PID (it returns the initial PID):
Code:

+ eval echo 34209998
echo 34209998
++ echo 34209998
34209998

Executing the command manually (without using the variable) gives the PID correctly:
Code:

+ ps -fu sbi
+ grep ExternalPurgeConsole.sh
+ grep -v grep
+ awk '{ print $2 }'
18874528


Here is the complete script and complete ouput:
Code:

    +1  #!/bin/bash
    +2  set -xv
    +3  #Purge process
    +4  #full process line
    +5  PROC_EXT_PURGE=`ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep`
    +6  #PID only
    +7  PID_EXT_PURGE=`ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep|awk '{ print $2 }'`
    +8
    +9  echo ""
  +10  echo "############################"
  +11  echo "# CHECKING EXTERNAL PURGE  #"
  +12  echo "############################"
  +13  echo ""
  +14
  +15  if [ `echo $PID_EXT_PURGE|wc -l` == 1 ]
  +16  then
  +17  echo "During the check, the external purge was running under the below process:"
  +18  echo $PROC_EXT_PURGE
  +19  echo ""
  +20  /appl/edi/IBM/SBI/bin/control_extpurge.sh stop
  +21  echo ""
  +22  sleep 5
  +23  /appl/edi/IBM/SBI/bin/control_extpurge.sh start
  +24  echo ""
  +25  sleep 5
  +26  #getting new pid and process without variable
  +27  ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep|awk '{ print $2 }'
  +28  #eval variable to try to get new PID and PROCESS
  +29  eval echo "$PID_EXT_PURGE"
  +30  eval echo "$PROC_EXT_PURGE"
  +31  fi

Code:

#Purge process
#full process line
PROC_EXT_PURGE=`ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep`
ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep
++ ps -fu sbi
++ grep ExternalPurgeConsole.sh
++ grep -v grep
+ PROC_EXT_PURGE='    sbi 34209998        1  0 14:23:25  pts/0  0:00 /bin/sh /appl/edi/IBM/SBI/bin/ExternalPurgeConsole.sh -clearlocks -nolockwait'
#PID only
PID_EXT_PURGE=`ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep|awk '{ print $2 }'`
ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep|awk '{ print $2 }'
++ ps -fu sbi
++ grep ExternalPurgeConsole.sh
++ grep -v grep
++ awk '{ print $2 }'
+ PID_EXT_PURGE=34209998

echo ""
+ echo ''

echo "############################"
+ echo '############################'
############################
echo "# CHECKING EXTERNAL PURGE  #"
+ echo '# CHECKING EXTERNAL PURGE  #'
# CHECKING EXTERNAL PURGE  #
echo "############################"
+ echo '############################'
############################
echo ""
+ echo ''


if [ `echo $PID_EXT_PURGE|wc -l` == 1 ]
then
echo "During the check, the external purge was running under the below process:"
echo $PROC_EXT_PURGE
echo ""
/appl/edi/IBM/SBI/bin/control_extpurge.sh stop
echo ""
sleep 5
/appl/edi/IBM/SBI/bin/control_extpurge.sh start
echo ""
sleep 5
#getting new pid and process without variable
ps -fu sbi |grep ExternalPurgeConsole.sh|grep -v grep|awk '{ print $2 }'
#eval variable to try to get new PID and PROCESS
eval echo "$PID_EXT_PURGE"
eval echo "$PROC_EXT_PURGE"
fi
echo $PID_EXT_PURGE|wc -l
++ echo 34209998
++ wc -l
+ '[' 1 == 1 ']'
+ echo 'During the check, the external purge was running under the below process:'
During the check, the external purge was running under the below process:
+ echo sbi 34209998 1 0 14:23:25 pts/0 0:00 /bin/sh /appl/edi/IBM/SBI/bin/ExternalPurgeConsole.sh -clearlocks -nolockwait
sbi 34209998 1 0 14:23:25 pts/0 0:00 /bin/sh /appl/edi/IBM/SBI/bin/ExternalPurgeConsole.sh -clearlocks -nolockwait
+ echo ''

+ /appl/edi/IBM/SBI/bin/control_extpurge.sh stop
killing external purge
+ echo ''

+ sleep 5
+ /appl/edi/IBM/SBI/bin/control_extpurge.sh start
Starting HPPurge...
External Purge started
Successfully started
+ echo ''

+ sleep 5
+ ps -fu sbi
+ grep ExternalPurgeConsole.sh
+ grep -v grep
+ awk '{ print $2 }'
18874528
+ eval echo 34209998
echo 34209998
++ echo 34209998
34209998
+ eval echo '    sbi 34209998        1  0 14:23:25  pts/0  0:00 /bin/sh /appl/edi/IBM/SBI/bin/ExternalPurgeConsole.sh -clearlocks -nolockwait'
echo      sbi 34209998        1  0 14:23:25  pts/0  0:00 /bin/sh /appl/edi/IBM/SBI/bin/ExternalPurgeConsole.sh -clearlocks -nolockwait
++ echo sbi 34209998 1 0 14:23:25 pts/0 0:00 /bin/sh /appl/edi/IBM/SBI/bin/ExternalPurgeConsole.sh -clearlocks -nolockwait
sbi 34209998 1 0 14:23:25 pts/0 0:00 /bin/sh /appl/edi/IBM/SBI/bin/ExternalPurgeConsole.sh -clearlocks -nolockwait


rtmistler 01-09-2018 12:07 PM

Perhaps you can post the full content of your script here within [code][/code] tags so as to preserve formatting.

You might be getting the PID of your ps command and not the PID of the process.sh that you wanted. Have you verified that it is obtaining the PID correctly in the first place?

Suggest you add some debug to your script to help, add "set -xv" just after the #!/bin/bash line to enable some more debug.

BW-userx 01-09-2018 12:07 PM

it will not have a PID until it starts. the first letter says that. Process .. not running no process of that application/script is going on so no PID

a little different way to get PID using BASH.
Code:

#!/bin/bash
#gets script name plus ./
name=$0
#strips off the ./
name=${name#*/}
#gets the PID of script no matter what its name is
PID="$(pgrep -u $USER "$name" )"
echo "name=: $0 and pid is $PID"

results
Code:

userx@solus ~ $ ./getPID
name=: ./getPID and pid is 2681


BW-userx 01-09-2018 12:53 PM

I reread and have this bash function I wrote awhile back and it is / should be self explanatory.
Code:


#kill the prior pids leaving the new one
function killoldPid(){
# add the script name <script name> remove the < > brackets also, when adding the what ever you named this script
if [ $(pgrep -u $USER setbg3 | wc -l) -ge 2 ] && [ $(pgrep -u $USER sleep | wc -l ) -ge 1 ]; then
    {
        printf "$(pgrep -u $USER setbg3 | wc -l) "pid# --" $(pgrep -u $USER sleep | wc -l)\n"
        pkill -u $USER -o sleep
        pkill -u $USER -o setbg3 # <--you need to add what you call this script here <script-name>
    }
else
    {
        # add script name here too
        printf "setbg3 pid $(pgrep -u $USER setbg3 ) sleep pid $(pgrep -u $USER sleep)\n"
    }
fi

}

if you know how to use vars then you can mod it to maybe work for you.

keefaz 01-09-2018 01:36 PM

If getting pid for process.sh is a repeatable thing, I would edit process.sh so it writes its pid in a file (for example in /var/tmp) each time it's launched

MadeInGermany 01-09-2018 03:37 PM

You must redo the evaluation in the PID= assignment!
Code:

PID=`ps -fu user |grep process.sh|grep -v grep|awk '{ print $2 }'`
or (better) the equivalent
Code:

PID=`pgrep -fu user process.sh`

oimoit 01-10-2018 08:42 AM

I edited my first post with the script and ouput in debug mode.
Hope it is more understandable. (issue is not with the command to find the PID.. that works.. it is to re-evaluate the variable command after to get the new PID)

MadeInGermany 01-10-2018 09:48 AM

You seem to think the variable stores the commands in backticks.
No, it only stores the result, a simple text string.
An eval of a simple text string does nothing.
Code:

PROC_EXT_PURGE=`ps -fu sbi | grep -w "ExternalPurgeConsole[.]sh"`
PID_EXT_PURGE=`echo "$PROC_EXT_PURGE" | awk '{print $2}"`
if [ -n "$PID_EXT_PURGE" ]
then
  echo "During the check, the external purge was running under the below process:"
  echo "$PROC_EXT_PURGE"
  echo ""
  /appl/edi/IBM/SBI/bin/control_extpurge.sh stop
  echo ""
  sleep 5
  /appl/edi/IBM/SBI/bin/control_extpurge.sh start
  echo ""
  sleep 5
  #getting new pid and process
  PROC_EXT=`ps -fu sbi | grep -w "ExternalPurgeConsole[.]sh"`
  PID_EXT=`echo "$PROC_EXT" | awk '{print $2}"`
  echo "The new process:
$PROC_EXT
"
fi

--
You can store the commands in a function
Code:

proc_ext(){
  ps -fu sbi | grep -w "ExternalPurgeConsole[.]sh"
}

and call it twice
Code:

PROC_EXT_PURGE=`proc_ext`
...
PROC_EXT=`proc_ext`


oimoit 01-10-2018 10:56 AM

Quote:

Originally Posted by MadeInGermany (Post 5804684)
--
You can store the commands in a function
Code:

proc_ext(){
  ps -fu sbi | grep -w "ExternalPurgeConsole[.]sh"
}

and call it twice
Code:

PROC_EXT_PURGE=`proc_ext`
...
PROC_EXT=`proc_ext`



That did the trick. I wanted to make sure the command itself would be modified once only if need to be.

This way, it is stored in the function, I call them up later.

Thanks!

MadeInGermany 01-10-2018 11:45 AM

Note that the [.] fixes two things:
1. it requires a literal dot, while just a . means "any character"
2. the [ ] show up in the ps -f output, so the grep will not find its own argument. You do not need an ugly | grep -vw grep

keefaz 01-10-2018 12:44 PM

Nice tip, thanks


All times are GMT -5. The time now is 07:59 PM.