RE: OT: bash vs. ksh subprocess counts

  • From: Herring Dave - dherri <Dave.Herring@xxxxxxxxxx>
  • To: Jared Still <jkstill@xxxxxxxxx>
  • Date: Tue, 24 Apr 2012 14:02:33 +0000

Thx Jared - I appreciate the followup and sharing different methods!
I meant to initially share that the original solution is flawed and my intent 
is not to look for better methods of doing this but more for understanding 
details about the differences in ksh and bash with this particular situation.  
As others have suggested, I removed the count ("-c") from the if-test just to 
see better what's going on.

In bash the commands:

set -x
ps -ef|grep -w $0
if [ "`ps -ef|grep -w $0`" > "3" ]; then
   exit 1

... display:

+ ps -ef
+ grep -w ./
oracle   24865 28645  0 09:33 pts/0    00:00:00 /bin/bash ./
oracle   24869 24865  0 09:33 pts/0    00:00:00 grep -w ./
++ ps -ef
++ grep -w ./
+ '[' 'oracle   24865 28645  0 09:33 pts/0    00:00:00 /bin/bash ./
oracle   24870 24865  0 09:33 pts/0    00:00:00 /bin/bash ./
oracle   24872 24870  0 09:33 pts/0    00:00:00 grep -w ./' ']'
+ exit 1

In ksh the same commands display:

+ ps -ef
+ grep -w ./
oracle   24954 28645  0 09:33 pts/0    00:00:00 /bin/ksh ./
oracle   24956 24954  0 09:33 pts/0    00:00:00 grep -w ./
+ ps -ef
+ grep -w ./
+ [ oracle   24954 28645  0 09:33 pts/0    00:00:00 /bin/ksh ./
oracle   24958 24954  0 09:33 pts/0    00:00:00 grep -w ./ ]
+ > 3
+ exit 1

So it appears that bash forks an extra process for a command stream within an 
if-test while ksh doesn't.  Replacing the command stream with a literal the 2 
shells match up.

Acxiom Corporation
EML   dave.herring@xxxxxxxxxx
TEL    630.944.4762
MBL   630.430.5988
1501 Opus Pl, Downers Grove, IL 60515, USA

[Friend Us on Facebook]<>  [Link Us on 
LinkedIn] <>   [Follow Us on 
Twitter] <>

[cid:[email protected]]
The information contained in this communication is confidential, is intended 
only for the use of the recipient named above, and may be legally privileged. 
If the reader of this message is not the intended recipient, you are hereby 
notified that any dissemination, distribution or copying of this communication 
is strictly prohibited. If you have received this communication in error, 
please resend this communication to the sender and delete the original message 
or any copy of it from your computer system. Thank you.

From: Jared Still [mailto:jkstill@xxxxxxxxx]
Sent: Friday, April 20, 2012 11:55 AM
To: Herring Dave - dherri
Cc: oracle-l@xxxxxxxxxxxxx
Subject: Re: OT: bash vs. ksh subprocess counts

On Sun, Apr 15, 2012 at 10:45 AM, Herring Dave - dherri 
<Dave.Herring@xxxxxxxxxx<mailto:Dave.Herring@xxxxxxxxxx>> wrote:
I realize this is way off topic (probably should be titled "WOT:...") for 
Oracle but #1, I don't currently belong to any good linux/redhat forums and #2, 
the issue found was from an Oracle maint. script :-)

WOT = Wide Open Throttle

Sometimes applies to Oracle I guess.

Anyway, I'm on a RHEL 4.6 server (2.6.9-67.0.22.ELlargesmp) and noticed a given 
DBA maint script wasn't running.  It turns out that the only difference between 
when it last ran and now is someone changed the shell to be "bash" -> 
#!/bin/bash vs. #!/bin/ksh.  The script has a little if-test to start with, 
used as a way to determine if a previous iteration of the job is still running:

if [ `ps -ef|grep -cw $0` -gt 3 ]; then
  echo "$0 is already running"
  exit 2

There are better methods for doing this.

Following is a function that creates a PID file and uses it for locking.
Stale locks are handled.

a possible issues with this -    not useful as is on cluster, as other nodes 
could run the script.

Anyway, it may be of some use.

Jared Still
Certifiable Oracle DBA and Part Time Perl Evangelist
Oracle Blog:
Home Page:
==== script =====

9:49-poirot:ts20:jkstill-22 > expand -t3


function script_lock  {
   typeset MY_LOCKFILE

   # remove stale lockfile
   [ -r "$MY_LOCKFILE" ] && {
      PID=$(cat $MY_LOCKFILE)
      ACTIVE=$(ps --no-headers -p $PID)
      if [ -z "$ACTIVE" ]; then
         rm -f $MY_LOCKFILE

   # set lock

   if (set -o noclobber; echo "$$" > "$MY_LOCKFILE") 2> /dev/null; then
      trap 'rm -f "$MY_LOCKFILE"; exit $?' INT TERM EXIT
      return 0
      echo "Failed to acquire $LOCKFILE. Held by $(cat $LOCKFILE)"
      exit 1

function script_unlock {
   rm -f "$LOCKFILE"
   trap - INT TERM EXIT

script_lock $LOCKFILE

echo press '<ENTER>...'
read dummy



Other related posts: