Script to restart process

HI,

I am trying to write a scrip which would restart active process. This is what i have written till now.

[xmp@tswebptcadmin ~]$ xms show pr PRESE.*
NAME                            STATUS    ROLE  PID    RSTRTS  STARTED       MACHINE
-----------------------------------------------------------------------------------------
PRESENCE-STORE.1.tswebptcxmp1   Running   A     29002       0  Aug-27 20:58  tswebptcxmp1
PRESENCE-STORE.1.tswebptcxmp2   Running   S     16097       0  Aug-27 21:05  tswebptcxmp2
PRESENCE-STORE.2.tswebptcxmp1   Running   A     29003       0  Aug-27 20:58  tswebptcxmp1
PRESENCE-STORE.2.tswebptcxmp2   Running   S     16098       0  Aug-27 21:05  tswebptcxmp2
#!/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
#########################################################################################################

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=`xms show pr PRESENCE-STORE.* |awk '{print $3}'|grep "A"`

#Active PSE_COUNT
#-----------------
if [ $ACTIVE_PSE_COUNT -gt 200 ]; then
xms restart proc PRESENCE-STORE.1.tswebptcxmp1

fi

exit

Here if the value for ACTIVE_PSE_COUNT exceed beyond 200 then it should restart active process.

Here i want to restart active process which has value "A" under role, which i am not sure how to do it. Please advice.

-Siddhesh

---------- Post updated at 10:32 AM ---------- Previous update was at 10:11 AM ----------

Hi All,

Further to this.

#!/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 '{print $1 $3}' |grep "A" |tail -1`

#Active PSE_COUNT
#-----------------
if [ $ACTIVE_PSE_COUNT -gt 200 ]; then
xms restart proc $ACTIVE_PSE_PROC_1

fi

exit

Now what issue i m facing is the out of ACTIVE_PSE_PROC_1 is as below...

PRESENCE-STORE.1.tswebptcxmp1A

I do not want the last letter "A" here, because of which it does not reastar the process. Please advice.

[xmp@tswebptcadmin ~]$ ./radiusmon.sh
ERROR: No such process: PRESENCE-STORE.1.tswebptcxmp1A

-Siddhesh

Hi

ACTIVE_PSE_PROC_1=`xms show pr PRESENCE-STORE.1.*  | awk '$3=="A"{print $1}' | tail -1`

Guru.

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 :

ACTIVE_PSE_PROC_1=`xms show pr PRESENCE-STORE.1.* |awk '$3 ~ /A/ {print $1 $3}' |tail -1`

Hi,

I got that working. Now the problem is

/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`

/home/xmp/bin/radstats > /home/xmp/bin/radstats_output.txt

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.

-SIddhesh

In what way is it "not working"?

How is this shell script being run? Manually by you, or in cron?

Are you perhaps getting a different shell in the script than you do in your login?

Hi,

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

-Siddhesh

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.

I am running this with "xmp" user.

so adding . /etc/profile will solve this?

-Siddhesh

---------- Post updated at 11:05 PM ---------- Previous update was at 10:58 PM ----------

Well I added the mentioned line, but

it is working when i run the script manually, but not when it is through cron.

-Siddhesh

In cron, the login, or both?

In what way is it "not working"? How do you know it's being run at all? Does it print any errors?

Try putting this below the shebang:

exec 2> /tmp/error.log

so that error messages get put into that file for you to read, instead of being given to cron which may or may not mail them to you.

If that file is not created, then I suspect the cron entry simply isn't being run at all.