Re: Threading ??? Shell Scripts

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I'm attaching a script (waaay at the bottom), "DISPATCH", which manages
up to N simultaneous jobs.

Use via something like:

	#!/bin/ksh
	export DISPATCH_MAX = 6      # six jobs at once
	DISPATCH CLEAR               # clear out the bookkeeping directory
	DISPATCH SYNC                # make sure no jobs are still outstanding
	DISPATCH /tmp t01 job1
	DISPATCH /tmp t02 job2
	DISPATCH /tmp t03 job3
	DISPATCH /tmp t04 job4
	DISPATCH /tmp t05 job5
	DISPATCH /tmp t06 job6
	DISPATCH /tmp t07 job7
	DISPATCH /tmp t08 job8
        DISPATCH SYNC
        DISPATCH /tmp t09 job9
        DISPATCH SYNC
        DISPATCH /tmp t10 jobA
        DISPATCH /tmp t11 jobB
        DISPATCH SYNC
	echo "All finished, sir."
        exit

In the above example, job{1,2,3,4,5,6} will be started.  As soon as any one
of those finishes, job7 will be started.  As soon as the next job finishes,
job 8 will start.  The "DISPATCH SYNC" after job8 says that all outstanding
jobs have to finish before "DISPATCH SYNC" will return.  After the SYNC,
job9 will run by itself (single threaded, waited upon by the SYNC after
job9).  When job9 finishes, both jobA and jobB will be started.  As soon
as both jobA and jobB finish, the last DISPATCH SYNC will finish and the
script will exit.

Hope this helps'idly,

-S

Brian D. McGrew wrote:
> 
> Is there any way to sort of make a shell script threaded???
> 
> Something like...
> 
> #!/bin/sh
> 
> Func1()
> {
> 	Param=$1
> }
> 
> Func2()
> {
> 	Param=$1
> }
> 
> Func1 test1 &
> Func2 test2 &
> Func1 test &
> 
> ???
> 
> I think I got the write concept but bad implementation???
> 
> :b!
> 
> Brian D. McGrew { brian@xxxxxxxxxxxxx || brian@xxxxxxxxxxxxxxxxxxx }
> --
> > This is a test.  This is only a test!
>   Had this been an actual emergency, you would have been
>   told to cancel this test and seek professional assistance!
> 
> 
> -- 
> fedora-list mailing list
> fedora-list@xxxxxxxxxx
> To unsubscribe: https://www.redhat.com/mailman/listinfo/fedora-list
> 


############## script starts on next line ####################
#!/bin/ksh
DOIT ()
{
  DIR=$1
  TOUCHME=$DISPATCH_DIR/$2
  shift 2
  CMD=$*
  TF="cd $DIR; $CMD"
  TS=`date +%T`

  rm -f $TOUCHME.PASS $TOUCHME.FAIL
  echo "[$TS] $TF" >  $TOUCHME.ACTIVE
  if [ -d $DIR ]
  then echo "+ cd $DIR"
       cd $DIR
       echo "+ $CMD"
       if $CMD
       then echo "[$TS->`date +%T`] $TF" >  $TOUCHME.PASS
            echo "PASS [$TS->`date +%T`] $TF" >> $HOME/DISPATCH_HISTORY
       else echo "[$TS->`date +%T`] $TF" >  $TOUCHME.FAIL
            echo "FAIL [$TS->`date +%T`] $TF" >> $HOME/DISPATCH_HISTORY
       fi
  else echo "Directory \"$DIR\" not found."
       echo "Unable to execute \"$CMD\"."
       echo "[$TS->`date +%T`] $TF" >  $TOUCHME.FAIL
       echo "FAIL [$TS->`date +%T`] $TF" >> $HOME/DISPATCH_HISTORY
  fi

  rm -f $TOUCHME.ACTIVE
}

# MAIN Main main

#  DISPATCH
#
#  Format: 
#       DISPATCH CLEAR
#       DISPATCH SYNC
#       DISPATCH CDIR TF CMD
#
#       where:
#               CLEAR           - Remove the tracking flag directory ($DISPATCH_DIR)
#               SYNC            - Wait for active processes to finish before exiting
#               CDIR            - Directory to execute CMD in
#               TF              - Tracking flag (actually file $DISPATCH_DIR/$TF)
#               CMD             - The command to execute
#
# Enviornment vars:
#
#       DISPATCH_DIR            - A directory to hold tracking flag files
#                                 Default is $HOME/DISPATCH
#
#       DISPATCH_MAX            - Maximum number of active tasks dispatched
#                                 Default is 4

if [ "X$DISPATCH_MAX" = "X" ]
then integer DISPATCH_MAX=4
fi
export DISPATCH_MAX
if [ "X$DISPATCH_DIR" = "X" ]
then DISPATCH_DIR=$HOME/DISPATCH
fi
export DISPATCH_DIR
if [ ! -d $DISPATCH_DIR ]
then mkdir $DISPATCH_DIR
fi

if [ -f $HOME/STOP ]
then echo "File $HOME/STOP exists... skipping \"DISPATCH $*\""
     exit -1
fi


if [ "X$1" = "XSYNC" ]
then echo "**********************************************************"
     echo "* DISPATCH SYNC -- STARTED                               *"
     echo "**********************************************************"
     while [ `ls -l $DISPATCH_DIR/*.ACTIVE 2>/dev/null | wc -l` -ne 0 ]
     do
         sleep 3
     done
     echo "**********************************************************"
     echo "* DISPATCH SYNC -- FINISHED                              *"
     echo "**********************************************************"
     echo "SYNC [`date +%D` `date +%T`] $TF" >> $HOME/DISPATCH_HISTORY
else if [ "X$1" = "XCLEAR" ]
     then echo "+ DISPATCH CLEAR"
          echo "+ rm -rf $DISPATCH_DIR"
          echo "CLEAR [`date +%D` `date +%T`] $TF" >> $HOME/DISPATCH_HISTORY
          rm -rf $DISPATCH_DIR
     else while [ `ls -l $DISPATCH_DIR/*.ACTIVE 2>/dev/null | wc -l` -ge $DISPATCH_MAX ]
          do
            sleep 3
          done
          if [ -f $HOME/STOP ]
          then echo "File $HOME/STOP exists... skipping \"DISPATCH $*\""
               exit -1
          else DOIT $* &
               sleep 2   # Allow file system to catch up with $DISPATCH_DIR
          fi
     fi
fi


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux