Skip to content

Instantly share code, notes, and snippets.

@shanefreeman
Last active August 20, 2016 00:25
Show Gist options
  • Select an option

  • Save shanefreeman/ae7ee360554cf08171ae7cd05801a993 to your computer and use it in GitHub Desktop.

Select an option

Save shanefreeman/ae7ee360554cf08171ae7cd05801a993 to your computer and use it in GitHub Desktop.
This will run top the specified number of iterations, at the specified interval. This is useful for troubleshooting processes that are hogging CPU/Memory, which a qkview won't tell you.
#!/bin/bash
# USE AT YOUR OWN RISK!!!
#
# play nice and clean up on exit
function exitcleanup {
if [ $success ]
then
echo -e "\\n--------------- $end iterations every $sleep seconds completed successfully ---------------"
echo -e "\\n--------------- $end iterations every $sleep seconds completed successfully ---------------" >> $output
else
echo -e "\\n--------------- Interrupted at $i of $end every $sleep seconds ---------------"
echo -e "\\n--------------- Interrupted at $i of $end every $sleep seconds ---------------" >> $output
fi
test $restoretopsettings == 1 && mv -f $HOME/.toprc.bak $HOME/.toprc && echo -e "\\nrestored top configuration..."
test $removetopsettings == 1 && rm -f $HOME/.toprc && echo -e "\\nremoved top configuration..."
echo -e "\\nOutput file details:\\n$(ls -la $output)\\n"
rm -f $LOCKFILE
exit 0
}
#
# set lockfile path
LOCKFILE=/var/tmp/top.sh.lockfile
#
# check to be sure this script isn't already running
if [ -f $LOCKFILE ]
then
echo -e "Lockfile exists!:\\n$LOCKFILE\\nIs this script already running?!"
exit 1
fi
#
if [ $(whoami) != 'root' ]
then
echo "You probably should be root user for this!"
exit 1
fi
#
# set some variables
start=1
i=$start
end="$1"
sleep="$2"
restoretopsettings=0
removetopsettings=0
minparams=2
#
# check to make sure user set the proper number of command line arguements
if [ $# -lt "$minparams" ]
then
echo -e "This script needs at least $minparams command-line arguments!\\nI need a [number of loops], and a [sleep interval] to work.\\nTry:\\n # ./top.sh 60 60"
exit 1
fi
#
# set some variables
startdate=`date +%F_%T`
output="/var/tmp/_`echo $startdate`_`echo $HOSTNAME`_top-script.out"
#
# attempt to create files we need
touch $LOCKFILE
touch $output
#
# configure cleanup after making changes to file system
trap exitcleanup EXIT
#
# check the files were created
if [ ! -w $output ] && [ ! -w $LOCKFILE ]
then
echo -e "Need write access to /var/tmp/ to continue"
exit 1
fi
#
# top config file in base64
configtop="echo UkNmaWxlIGZvciAidG9wIHdpdGggd2luZG93cyIgIyBzaGFtZWxlc3MgYnJhZ2dpbicKSWQ6YSwgTW9kZV9hbHRzY3I9MCwgTW9kZV9pcml4cHM9MSwgRGVsYXlfdGltZT0zLjAwMCwgQ3Vyd2luPTAKRGVmIGZpZWxkc2N1cj1BRUhJT1FUV0tOTWJjZGZnSnBscnN1dnl6WAp3aW5mbGFncz0zMTU0NSwgc29ydGluZHg9MTAsIG1heHRhc2tzPTAKc3VtbWNscj02LCBtc2dzY2xyPTEsIGhlYWRjbHI9NiwgdGFza2Nscj02CkpvYiBmaWVsZHNjdXI9QUJjZWZnamxyc3R1dnl6TUtOSElXT1BRRFgKd2luZmxhZ3M9NjI3NzcsIHNvcnRpbmR4PTAsIG1heHRhc2tzPTAKc3VtbWNscj02LCBtc2dzY2xyPTYsIGhlYWRjbHI9NywgdGFza2Nscj02Ck1lbSBmaWVsZHNjdXI9QU5PUFFSU1RVVmJjZGVmZ2psbXl6V0hJS1gKd2luZmxhZ3M9NjI3NzcsIHNvcnRpbmR4PTEzLCBtYXh0YXNrcz0wCnN1bW1jbHI9NSwgbXNnc2Nscj01LCBoZWFkY2xyPTQsIHRhc2tjbHI9NQpVc3IgZmllbGRzY3VyPUFCREVDR2ZoaWpsb3BxcnN0dXZ5ek1LTldYCndpbmZsYWdzPTYyNzc3LCBzb3J0aW5keD00LCBtYXh0YXNrcz0wCnN1bW1jbHI9MywgbXNnc2Nscj0zLCBoZWFkY2xyPTIsIHRhc2tjbHI9Mwo=|base64 -di - > $HOME/.toprc"
#
# Backup exiting top settings if they exist, writing top config to display all cpu's/cpu # column
if [ -s $HOME/.toprc ]
then
echo "Backing up existing top settings..."
mv -f $HOME/.toprc $HOME/.toprc.bak
echo -e "...top settings backed up!\\nConfiguring top..."
eval $configtop
echo -e "...top configured.\\n"
restoretopsettings=1
else
echo -e "No existing top settings to back up, configuring top..."
eval $configtop
echo -e "...top configured"
removetopsettings=1
fi
#
# header on output file
echo -e "TOP output for host: $HOSTNAME\\n" >> $output
echo -e "Initially ran for $end iterations every $sleep seconds." >> $output
#
# Telling user they can stop it early with ^C
echo -e "Running for $end iterations every $sleep seconds, else press CTRL+C to stop.\\n"
#
# finally, the actual loop that does the work
command="top -cbn1"
for ((i=$start; i<=$end; i++))
do
echo "***`date +%k:%M:%S` - Running '$command', iteration '$i' of '$end', and sleeping '$sleep' seconds...***"
echo -e "\\n***********" >> $output
date >> $output
$command >> $output
sleep $sleep
done
#
# successful finish:
success=1
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment