Showing posts with label Shell Script. Show all posts
Showing posts with label Shell Script. Show all posts

Thursday, January 12, 2012

bash get last modified time file

#!/bin/bash
#########################################################################
## Name    :    get-file-modification.sh
## Author  :    Saurabh Gupta
## Date    :    AM 09:25 12 January 2011
## Desc    :    bash get last modified time file
## Source  :    http://ccplusplus.com/p/shell-scipt.html
## Note    :
#########################################################################
if [ "x$1" == "x" ];then
   echo "file name missing !!!"
   echo "$0 <file-name>"
   exit
fi
MODIFICATION_TIME=`ls -l $1`

GET_TIME_MONTH=`echo $MODIFICATION_TIME | awk '{print $6}'`
DAY=`echo $MODIFICATION_TIME | awk '{print $7}'`
HOUR_MIN=`echo $MODIFICATION_TIME | awk '{print $8}'`
HOUR=`echo $HOUR_MIN | cut -d ':' -f1`
MIN=`echo $HOUR_MIN | cut -d ':' -f2`

echo $GET_TIME_MONTH $DAY $HOUR $MIN

##############################################################
#    OUTPUT
##############################################################
#
#[sgupta@rhel6x64 scripts]$ ls -ltr get-cpu-usage.sh
#-rw-rw-r--. 1 sgupta sgupta 662 Nov  1 22:38 get-cpu-usage.sh
#[sgupta@rhel6x64 scripts]$
#
###############################################################
# [sgupta@rhel6x64 scripts]$ ./get-file-modification get-cpu-usage.sh
# Nov 1 22 38
# [sgupta@rhel6x64 scripts]$
##############################################################

Monday, January 2, 2012

shell script to kill a process by name

#!/bin/bash
#########################################################################
## Name    :    kill-process-by-name.sh
## Author  :    Saurabh Gupta
## Date    :    PM 07:05 01 January 2012
## Desc    :    kill process by name in unix
## Source  :    http://www.ccplusplus.com/p/shell-scipt.html
## Note    :
#########################################################################

echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo " killing process :- $1"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

if [ "$1" == "" ];then
   echo "Usage : ./pkill.sh <process name>"
else
     for i in `ps ax | grep $1 | grep -v grep | sed 's/ *//' | sed 's/[^0-9].*//'`
        do
           kill -9 $i
        done
fi

Wednesday, December 21, 2011

getopts example in shell script

################################################################
# File          :   getopts.sh
# Author        :   Saurabh Gupta
# Created       :   10:16 AM 12/21/2011
# Description   :   getopts example in shell script
# Source        :   http://www.ccplusplus.com/p/shell-scipt.html
# Note          :   
################################################################

function usage () {
    echo -e "Usage: $0 -e \"EXAMPLE\"  -d \"DESCRIPTION\"  -s \"SOURCE\" "
}

clear

while getopts "e:d:s:" ARG;
    do
    case "${ARG}" in
        e) EXAMPLE="${OPTARG}";;
        d) DESCRIPTION="${OPTARG}";;
        s) SOURCE="${OPTARG}";;
    esac;
done

[ -z "${EXAMPLE}" ] && { usage && exit 1; }

echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "$EXAMPLE by $DESCRIPTION from $SOURCE"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"


#
# OUTPUT
#
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#getopts example by 'Saurabh Gupta' from http://www.ccplusplus.com/
#XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
#

Friday, October 14, 2011

shell script to get kernel version

#########################################################################
## Name    :    get-kernel-version.sh
## Author  :    Saurabh Gupta
## Date    :    AM 09:25 14 October 2011
## Desc    :    retrieves the kernel version of the linux
## Source  :    http://ccplusplus.com/p/shell-scipt.html
## Note    :
#########################################################################

echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
echo "XXXXXXX          Retrieving the Kernel Information          XXXXXXX"
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

version=`uname -r | awk -F. '{ printf("%d.%d.%d\n",$1,$2,$3); }'`
echo " "
printf "Kernel Version : %s\n" $version
echo " "
echo "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"


Monday, September 5, 2011

shell script to kill a process by name


####################################################
#  Author  : Saurabh Gupta                         #
#  Created : 10:30 AM 9/5/2011                     #
#  Killing a process by its name                   #
#  change your binary name in the scripts and run  #
#                                                  #
#  Usage   : change <your_binary_name> with your   #
#            process or binary name                #
####################################################
#!/bin/bash

YOUR_PROCESS_NAME=your_binary_name



your_binary_name_process_id=`ps -eo pid,args | grep $YOUR_PROCESS_NAME | grep -v grep | cut -c1-6`

if [ "$your_binary_name_process_id" == "" ]; then
     echo "your_binary_name not running"
else
     echo "PID of "$YOUR_PROCESS_NAME " - "$your_binary_name_process_id
     kill -9 $your_binary_name_process_id
     echo "killing your_binary_name"
fi

shell script to get cpu usage

####################################################
#  Author  : Saurabh Gupta                         #
#  Created : 9:30 AM 9/5/2011                      #
#  shell script to calculate the cpu usage         #
#  It will display the idle % of the cpu           #
####################################################

#!/bin/bash

id=0; tot=0; p_id=0; p_tot=0;

for ((i=0; $i < 100; ++i )) ; do

    p_id=$id; p_tot=$tot;
    x=`cat /proc/stat | grep -w cpu`;
    id=`echo $x | cut -d " " -f 5`;
    tot=0;
    for f in `echo $x`; do
        tot=$(($tot + $f));
    done;

    echo "idle cpu usage% = "$((($id - $p_id)*100/($tot - $p_tot)));

    sleep 3;
done