LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   "ps -ef" using in backquotes (https://www.linuxquestions.org/questions/solaris-opensolaris-20/ps-ef-using-in-backquotes-304275/)

porzer 03-21-2005 08:30 AM

"ps -ef" using in backquotes
 
Hi,

I have a question about using the ps within backqoutes (``)
When I tried to write a script which is supposed to run just once at a time
I encountered the following thing which I don't know if it's a BUG or FEATURE.

inside the script 'runonce.sh':
---begin script---
#! /bin/bash
# just run the ps and watch te output
ps -ef | grep 'runonce.sh'

RESULT_FULL=`ps -ef | grep 'runonce.sh'`
echo result_full: "$RESULT_FULL"

---end script---

this script outputs:
[hostnix] ./runonce.sh
oracle 6443 3691 0 15:29:02 pts/8 0:00 /bin/bash ./runonce.sh
result_full: oracle 6462 6443 0 15:29:02 pts/8 0:00 /bin/bash ./runonce.sh
oracle 6443 3691 0 15:29:02 pts/8 0:00 /bin/bash ./runonce.sh
oracle 6464 6462 0 15:29:02 pts/8 0:00 grep runonce.sh

why do I get the /bin/bash ./runonce.sh twice ??

any help welcome

guenter porzer

Technoslave 03-21-2005 08:38 AM

You're running the ps twice, that's why you're seeing it twice.

porzer 03-22-2005 12:54 AM

hi,

actually the ps is not running twice at the same time. It gets started in consecutive.
Besides what I don't understand is why in $RESULT_FULL I'm getting the runonce.sh script twice ?
If I run the same script in KSH( just changing #!/bin/bash to #!/bin/ksh )
I'm geting the expected result of 1 script process and 1 grep process.
As you can see in BASH I'm getting 3 processes 2 scripts running and 1 grep.

thanks
guenter porzer

ahh 03-22-2005 01:06 AM

If you want to know whats happening, add -x to /bin/bash.
Code:

#!/bin/bash -x
#just run the ps and watch te output

ps -ef | grep 'runonce.sh'

RESULT_FULL=`ps -ef | grep 'runonce.sh'`
echo result_full: "$RESULT_FULL"

Useful for debugging.

stickman 03-22-2005 12:34 PM

Are you asking why you see "/bin/bash" and "grep" lines? The grep line is the result of your script. You might try the "grep -v grep" trick or grep for [r]unonce.sh.

jlliagre 03-22-2005 01:16 PM

The OP is certainly asking why these two lines appears for a single script:
Code:

oracle 6462 6443 0 15:29:02 pts/8 0:00 /bin/bash ./runonce.sh
oracle 6443 3691 0 15:29:02 pts/8 0:00 /bin/bash ./runonce.sh

My guess would be that bash is either forking or firing a thread that appears to have the same command name as itself (before being updated by the loader possibly).

porzer 03-23-2005 03:20 AM

@ jlliagre, you are absolute right.

In the meantime I found a workaround for this behavior.
I just thougt there would be no difference between ksh and bash concerning backquotes ( or at least I've never read anything about this ).

Anyway thanks for all input for this topic !

guenter porzer


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