you use an treatment on every line but you took only the first line. It's more logical to put the head -1 before the awk treatement. Moreover the grep has the option "-m x" which stop reading file when the x patterns are found.
so the code should be :
ACTIVE_PSE_COUNT=`grep -m 1 'Current number of Active Requests to the PSE' /home/xmp/bin/radstats_output.txt | awk '{print $NF}' `
awk is a powerful tool, if you use a simple grep you must filter lines with awk :
For your code :
/home/xmp/bin/radstats > /home/xmp/bin/radstats_output.txt
ACTIVE_PSE_COUNT=`grep "Current number of Active Requests to the PSE" /home/xmp/bin/radstats_output.txt | awk '{print $NF}' | head -1`
ACTIVE_PSE_PROC_1=`xms show pr PRESENCE-STORE.1.* |awk '$3=="A"{print $1}'|tail -1`
ACTIVE_PSE_PROC_2=`xms show pr PRESENCE-STORE.2.* |awk '$3=="A"{print $1}'|tail -1`
It is working fine when i run in shell prompt, and redirects the output to home/xmp/bin/radstats_output.txt. But in script it is just not working. Please advice.
i am not able to redirect the output of "radstat" command to a file i mentioned. i am running this script through cron.
here is the complete script.
#!/bin/bash
#########################################################################################################
# Shellscript : radiusmon.sh - Monitoring radius start/stop accounting
# Version : V1.0
# Owner : Siddhesh Khavnekar <siddhesh.khavnekar@mobixell.com>
# Date : 2012-08-30
# Category : Files Management
#########################################################################################################
# Description : This scipt will Monitor radius start/stop accounting and restart active PSE
# processes if reached threshold values
#########################################################################################################
/home/xmp/bin/radstats > /home/xmp/bin/radstats_output.txt
ACTIVE_PSE_COUNT=`grep "Current number of Active Requests to the PSE" /home/xmp/bin/radstats_output.txt | awk '{print $NF}' | head -1`
ACTIVE_PSE_PROC_1=`xms show pr PRESENCE-STORE.1.* |awk '$3=="A"{print $1}'|tail -1`
ACTIVE_PSE_PROC_2=`xms show pr PRESENCE-STORE.2.* |awk '$3=="A"{print $1}'|tail -1`
#Active PSE_COUNT
#-----------------
if [ $ACTIVE_PSE_COUNT -gt 300 ]; then
xms restart proc $ACTIVE_PSE_PROC_1
#snmptrap -v 2c -c mobilewebproxy 10.201.1.99:162 '' 1.3.6.1.4.1.12702.9.10000.6 1.3.6.1.4.1.12702.9.10000 s "Current number of Active Requests to the PSE exceeded..Active PSE restarted!!"
fi
sleep 30
if [ $ACTIVE_PSE_COUNT -gt 300 ]; then
xms restart proc $ACTIVE_PSE_PROC_2
#snmptrap -v 2c -c mobilewebproxy 10.201.1.99:162 '' 1.3.6.1.4.1.12702.9.10000.6 1.3.6.1.4.1.12702.9.10000 s "Current number of Active Requests to the PSE exceeded..Active PSE restarted!!"
fi
exit
As what user? Does that user have permission to write to that file?
Also, cron has a very minimal PATH. Add this just below the shebang: . /etc/profile which should get a more sane PATH set so it can find things like grep.