LinuxQuestions.org
Help answer threads with 0 replies.
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 01-09-2018, 11:54 AM   #1
oimoit
LQ Newbie
 
Registered: Jan 2018
Posts: 3

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

Last edited by oimoit; 01-10-2018 at 08:41 AM. Reason: Clarifying
 
Old 01-09-2018, 12:07 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: MA, USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 7,997
Blog Entries: 13

Rep: Reputation: 3445Reputation: 3445Reputation: 3445Reputation: 3445Reputation: 3445Reputation: 3445Reputation: 3445Reputation: 3445Reputation: 3445Reputation: 3445Reputation: 3445
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.
 
Old 01-09-2018, 12:07 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,070

Rep: Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901
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

Last edited by BW-userx; 01-09-2018 at 12:24 PM.
 
Old 01-09-2018, 12:53 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: FreeBSD/Slackware-14.2+/ArcoLinux
Posts: 9,070

Rep: Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901Reputation: 1901
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.
 
Old 01-09-2018, 01:36 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
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
 
Old 01-09-2018, 03:37 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,178

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
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`
 
Old 01-10-2018, 08:42 AM   #7
oimoit
LQ Newbie
 
Registered: Jan 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
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)
 
Old 01-10-2018, 09:48 AM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,178

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
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`
 
1 members found this post helpful.
Old 01-10-2018, 10:56 AM   #9
oimoit
LQ Newbie
 
Registered: Jan 2018
Posts: 3

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
--
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!
 
Old 01-10-2018, 11:45 AM   #10
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,178

Rep: Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532Reputation: 532
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
 
2 members found this post helpful.
Old 01-10-2018, 12:44 PM   #11
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Nice tip, thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to evaluate variable values, details inside gglq000 Linux - Software 2 06-10-2016 10:55 AM
not able to evaluate variable mrajdeep Linux - Newbie 12 09-26-2013 05:57 PM
[SOLVED] How to evaluate the value of a variable ? 915086731 Linux - General 4 08-11-2011 05:03 AM
[bash] re-evaluate variable inside variable muzzol Programming 9 12-01-2010 11:31 AM
Basic Bash: How to use eval to evaluate variable names made of arbitrary strings. GrapefruiTgirl Programming 9 12-16-2009 10:25 AM

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

All times are GMT -5. The time now is 03:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration