• Categories

  • Archives

Unix Script to run commands repetitively

Sometime we need to repetitively run UNIX command after certain intervals.

The following simple UNIX shell script can be used in any scenario where you want to repetitively run the command.

Usage:

run_in_loop.sh “ls -lrt” 5

Runs ls –lrt every 5 seconds.

run_in_loop.sh “df -k” 10

Runs df –k every 10 seconds.
Help:

run_in_loop.sh -h

Script:

———–Script Starts——————————————————–

#!/bin/ksh

### Validate Input
check_input(){
$cmd > /dev/null 2>&1
if [ $? -ne 0 ];then
echo “Command ( $cmd ) couldn’t be executed …”
echo “Exiting….”
exit
fi

sleep $slp > /dev/null 2>&1
if [ $? -ne 0 ];then
echo “Error in sleep …”
echo “Exiting….”
exit
fi
}

#### Do processing
do_processing(){
while true
do
clear
$cmd
echo “To exit press “Ctrl C”…..”
sleep $slp
done
}

### Help
help(){
echo ”
The Purpose of this script is to run any executable command repetetively after certain number of seconds.

e.g.

$0 date 5 , will display date every 5 seconds.
$0 ” ls -lrt ” 2 will display files every 2 seconds.

If the command needs to be executed with options (multiple words), it needs to be passed in double quotes ” ”

exit
}

############### Main Main Main ###############
#
#
#
##############################################
. $HOME/.profile
export CURDIR=`echo $0 | awk -F’/’ ‘{split($0,fnamea,”/”);for (i=2;i<NF;i++) {PNAME=PNAME “/” fnamea[i]};if (PNAME==””) {print “.”} else {print PNAME}}’`
cd $CURDIR
cmd=$1
slp=$2

if [ “$1” == “-h” ]; then
help;
exit
fi

if [ $# -ne 2 ]; then
echo ” Usage…$0 “command” integer(sleep) ”
echo ” For help use .. $0 “-h” ”
exit
fi

check_input;
do_processing;
exit

———–Script Ends——————————————————–

Sample Output:

run_in_loop.sh “crs_stat -t” 5

Name           Type           Target    State     Host
————————————————————
ora….A1.inst application    ONLINE    ONLINE    server2
ora….A2.inst application    ONLINE    ONLINE    server1
ora.EA.db      application    ONLINE    ONLINE    server2
ora….SM1.asm application    ONLINE    ONLINE    server2
ora….R2.lsnr application    ONLINE    ONLINE    server2
ora….er1.gsd application    ONLINE    ONLINE    server2
ora….er1.ons application    ONLINE    ONLINE    server2
ora….er1.vip application    ONLINE    ONLINE    server2
ora….SM2.asm application    ONLINE    ONLINE    server1
ora….R1.lsnr application    ONLINE    ONLINE    server1
ora….er2.gsd application    ONLINE    ONLINE    server1
ora….er2.ons application    ONLINE    ONLINE    server1
ora….er2.vip application    ONLINE    ONLINE    server1
To exit press Ctrl C…..

run_in_loop.sh “ls -lrt /tmp/sample” 5

total 0
-rw-r–r–   1 oracle   dba               0 Mar 02 21:22 h
-rw-r–r–   1 oracle   dba               0 Mar 02 21:22 g
-rw-r–r–   1 oracle   dba               0 Mar 02 21:22 f
-rw-r–r–   1 oracle   dba               0 Mar 02 21:22 e
-rw-r–r–   1 oracle   dba               0 Mar 02 21:22 d
-rw-r–r–   1 oracle   dba               0 Mar 02 21:22 c
-rw-r–r–   1 oracle   dba               0 Mar 02 21:22 b
-rw-r–r–   1 oracle   dba               0 Mar 02 21:22 a
To exit press Ctrl C…..

Leave a comment