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 Code:
+ eval echo 34209998 Code:
+ ps -fu sbi Here is the complete script and complete ouput: Code:
+1 #!/bin/bash Code:
#Purge process |
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. |
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 Code:
userx@solus ~ $ ./getPID |
I reread and have this bash function I wrote awhile back and it is / should be self explanatory.
Code:
|
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
|
You must redo the evaluation in the PID= assignment!
Code:
PID=`ps -fu user |grep process.sh|grep -v grep|awk '{ print $2 }'` Code:
PID=`pgrep -fu user process.sh` |
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) |
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"` You can store the commands in a function Code:
proc_ext(){ Code:
PROC_EXT_PURGE=`proc_ext` |
Quote:
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! |
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 |
Nice tip, thanks
|
All times are GMT -5. The time now is 07:59 PM. |