Pacific-Design.com

    
Home Index

1. Bash

2. Concurrency

Bash / Concurrency /

Concurrent Processing using Bash


#!/bin/bash
#---------------------------------------------------------------------------------#
USERNAME='kevin'
PASSWORD='duraj'
DATABASE='engine19'

echo "$(date +"%Y-%m-%d %H:%M") - $DATABASE"
#---------------------------------------------------------------------------------#
repair_tables()
{
  TABLE_LIST=`mysql -u $USERNAME -p$PASSWORD -NB -e "show tables from $DATABASE"`
  counter=1

  for E in $TABLE_LIST
  do
      SQL="REPAIR TABLE $DATABASE.$E;"; echo "$SQL"

      #----------------------------------------------------#
      #              Concurent  Processing
      #----------------------------------------------------#
      isEvenNo=$( expr $counter % 5 )

      if [ $isEvenNo -ne 0 ]
      then
          # Background run 
          echo "Back: " "$SQL" 
          mysql -u $USERNAME -p$PASSWORD -NB -e  "$SQL" &
      else
          # Foreground run
          echo "Front:" "$SQL"
          RES=`mysql -u $USERNAME -p$PASSWORD -NB -e  "$SQL"`
          echo "$RES"
      fi
      (( counter++ ))
      #----------------------------------------------------#

      sleep 1;
  done
}
#---------------------------------------------------------------------------------#
optimize_tables()
{
  TABLE_LIST=`mysql -u $USERNAME -p$PASSWORD -NB -e "show tables from $DATABASE"`
  counter=1

  for E in $TABLE_LIST
  do
      SQL="OPTIMIZE TABLE $DATABASE.$E;"; echo "$SQL"

      #----------------------------------------------------#
      #              Concurent Processing
      #----------------------------------------------------#
      isEvenNo=$( expr $counter % 5 )

      if [ $isEvenNo -ne 0 ]
      then
          # Background run 
          echo "Back: " "$SQL" 
          mysql -u $USERNAME -p$PASSWORD -NB -e  "$SQL" &
      else
          # Foreground run
          echo "Front:" "$SQL"
          RES=`mysql -u $USERNAME -p$PASSWORD -NB -e  "$SQL"`
          echo "$RES"
      fi
      (( counter++ ))
      #----------------------------------------------------#

      sleep 1;
  done
}

#---------------------------------------------------------------------------------#
#---    Repair and optimize all tables in the following databases
#---------------------------------------------------------------------------------#

for db in $DATABASE
do  
    echo "Database: is [$db]"
    repair_tables $db
done

#---------------------------------------------------------------------------------#

for db in $DATABASE
do  
    echo "Database: is [$db]"
    optimize_tables $db
done
#---------------------------------------------------------------------------------#