It is currently Tue, 30 Nov 2021 17:49:30 GMT



 
Author Message
 Very bizarre /bin/sh behavior
OpenBSD 2.7

Script copied in verbatim:

=========================================================================
#!/bin/sh

PATH=/bin:/usr/bin:/usr/sbin:/sbin

PROCSTRING=$1

# Get the number of processes matching PROCSTRING and print it out.
NUMPROCS=`ps -Ujblaine | grep $PROCSTRING | grep -v grep | wc -l`
echo "NUMPROCS: $NUMPROCS"

exit
=========================================================================

jblaine : kickflop > ps -Ujblaine
  PID TT   STAT      TIME COMMAND
11264 ??  S       0:02.79 /usr/local/sbin/smbd -D
 6730 p0  T       0:00.01 sh -c sh
 8368 p0  T       0:00.45 tf (tf-40s1)
11696 p0  T       0:00.14 vi /usr/home/jblaine/.article
14787 p0  T       0:00.87 slrn
17910 p0  S       0:03.05 /usr/home/python/stable/bin/python ./new.py
20363 p0  Ss      0:00.76 -bash (bash)
29791 p0  R+      0:00.00 ps -Ujblaine
jblaine : kickflop > ./crap.sh foooooooooooooooooo
NUMPROCS:        4
jblaine : kickflop > ./crap.sh fkdsfjdsklfjklsdjflksdjfljksdlfjs
NUMPROCS:        4
jblaine : kickflop > ./crap.sh fkdsfjdsklfjklsdjflksdjfljksdlfjs
NUMPROCS:        2
jblaine : kickflop > ./crap.sh fkdsfjdsklfjklsdjflksdjfljksdlfjs
NUMPROCS:        1
jblaine : kickflop > ./crap.sh fkdsfjdsklfjklsdjflksdjfljksdlfjs
NUMPROCS:        2
jblaine : kickflop >

Can anyone explain THAT?



 Sat, 01 Feb 2003 03:00:00 GMT   
 Very bizarre /bin/sh behavior

When the shell script is running it is also a process shown by ps.
It has the name   /bin/sh ./crap.sh foooooooooooooooooo
so it counts to NUMPROCS
When the shells creates the piped processes it has to call fork to clone itself
In the beginning you have 5 processes named bin/sh ./crap.sh foooooooooooooooooo
The original shell script and the for piped processed.
When the ps process calls exec ps -Ujblaine it renames itself and ps
shows the remaining 4 bin/sh ./crap.sh foooooooooooooooooo.
If all other pipe processes call exec before the ps process does,
you only see the original shell script process and get the result 1.



 Sat, 01 Feb 2003 03:00:00 GMT   
 Very bizarre /bin/sh behavior

[...]

No.
I can't reproduce it either.

--
Christian "naddy" Weisgerber                          na...@mips.inka.de



 Sun, 02 Feb 2003 06:52:02 GMT   
 Very bizarre /bin/sh behavior

AUGH.

I've been bitten by this 3 times in the last N years, and everytime I
swear I'm not going to be bitten by it again.  And EVERYTIME I forget
to investigate that possibility (what you describe above).  So stupid
of me.

Thanks.



 Sun, 02 Feb 2003 09:27:34 GMT   
 Very bizarre /bin/sh behavior
In the referenced article, jbla...@kickflop.ne.mediaone.net (Jeff Blaine) writes:

You can sometimes use the simple trick of grepping for '[f]oo'
instead of 'foo' to stop processes counting themselves.  Here's what
I mean:

ancho.bath.ac.uk ?// ps -ax|grep 'foo'
19302 p1  S+      0:00.02 grep foo
ancho.bath.ac.uk ?// ps -ax|grep '[f]oo'
ancho.bath.ac.uk ?// ps -ax|grep 'foo'|wc -l
       1
ancho.bath.ac.uk ?// ps -ax|grep '[f]oo'|wc -l
       0

(The 'ancho.bath.ac.uk ?// ' is my obsolete prompt.  Don't ask :-)
--
Dennis Davis, BUCS, University of Bath, Bath, BA2 7AY, UK
D.H.Da...@bath.ac.uk



 Mon, 03 Feb 2003 03:00:00 GMT   
 
   [ 5 post ] 

Similar Threads

1. #!/bin/sh #!/usr/bin/sh can I do both for 2 diff machines

2. Strange behavior of /bin/sh..

3. Odd /bin/sh behavior on HP-UX 9.01

4. /bin/sh, /bin/ksh crash with SIGSEGV with a huge script

5. /bin/ksh vs /usr/xpg4/bin/sh

6. difference between /bin/sh and /sbin/sh

7. /bin/sh .vs. /sbin/sh

8. diff /sbin/sh and /bin/sh?

9. /bin/sh and /usr/bin/ksh

10. Any difference between /sbin/sh and /bin/sh?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software