HPE GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: While Loop Not Exit
Operating System - HP-UX
1833877
Members
1548
Online
110063
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2005 07:45 AM
03-28-2005 07:45 AM
I tested a wonderful script from sb. of this forum in an attempt to apply it to my situation. But I can not have the while loop end unless a hard keystroke of Ctrl+C is enforced. Syntac looks all right.
Here are the lines of script:
***************************************
#!/bin/sh
NOTIFIED=0
COMMAND=DEVP #test DEVP db up status
MAILBOX=zzzz@wwwyyy.com
CHECKINTERVAL=10 #launch every 10 seconds
while true
do
PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`
if test ${PROCS} -gt 1 # cannot use "-eq 1"
then
if test ${NOTIFIED} -eq 0
then
echo "Service ${COMMAND} is up again!!\n `date`" | mailx -s "Service Start
ed" ${MAILBOX}
NOTIFIED=1
fi
else
echo "Servie ${COMMAND} is down!!\n `date`" | mailx -s "Service Stopped" ${M
AILBOX}
NOTIFIED=0 # need comment out to avoid unlimited checking/mailxing
fi
sleep ${CHECKINTERVAL}
done
******************************************
All comments are appreciated!
Steven
Here are the lines of script:
***************************************
#!/bin/sh
NOTIFIED=0
COMMAND=DEVP #test DEVP db up status
MAILBOX=zzzz@wwwyyy.com
CHECKINTERVAL=10 #launch every 10 seconds
while true
do
PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`
if test ${PROCS} -gt 1 # cannot use "-eq 1"
then
if test ${NOTIFIED} -eq 0
then
echo "Service ${COMMAND} is up again!!\n `date`" | mailx -s "Service Start
ed" ${MAILBOX}
NOTIFIED=1
fi
else
echo "Servie ${COMMAND} is down!!\n `date`" | mailx -s "Service Stopped" ${M
AILBOX}
NOTIFIED=0 # need comment out to avoid unlimited checking/mailxing
fi
sleep ${CHECKINTERVAL}
done
******************************************
All comments are appreciated!
Steven
Steve
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2005 08:11 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2005 10:05 AM
03-28-2005 10:05 AM
Re: While Loop Not Exit
If I'm reading your script correctly, you want to
check, once every 10 seconds, if a command is
running and send a mail with the status.
How long you want this to run? In the current form,
this script would run forever. Now, if that's what
you want, you could run the script in the background
and it would keep doing the check you want. If
you want it to run for few hours, or want it to exit
under certain conditions, then you will have to insert
some code that checks this condition at the beginning
(or before end) of the "while" loop and exit.
- Biswajit
check, once every 10 seconds, if a command is
running and send a mail with the status.
How long you want this to run? In the current form,
this script would run forever. Now, if that's what
you want, you could run the script in the background
and it would keep doing the check you want. If
you want it to run for few hours, or want it to exit
under certain conditions, then you will have to insert
some code that checks this condition at the beginning
(or before end) of the "while" loop and exit.
- Biswajit
:-)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-28-2005 03:01 PM
03-28-2005 03:01 PM
Re: While Loop Not Exit
The script is designed to run forever. It keeps on monitoring the process in $COMMAND and sends mail when it stops, and continues monitoring. It even has a flag to send only one notice when the process stops. A script like this would be run in the background. A couple of notes:
PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`
has a basic problem. ps and grep are seldom reliable. While $COMMAND is set to "DEVP", the above statement can return several matches because grep never looks at the process name...it looks at the entire line. The grep will match DDEVP, /opt/DEVPDIR/prog and many other strings, whether it is a program name or a parameter. ps is so much more powerful than most admins know. Using ps -ef is a big load on a busy server so let's have ps do the searching in the process table. Change ps to do the searches with 100% accuracy:
COMMAND=DEVP
UNIX95= ps -C $COMMAND
Now ps always returns EXACTLY the number of processes that have a basename of DEVP. The UNIX95= construct is needed to enable 3 useful options: -C -H -o. ps always supplies a header as line #1, so we have to remember that wc -l is 1 higher than needed.
Throughout the script, the construct $(...) is used for "Command Substitution" except in the PROCS= statement. $(...) is always preferred, so the code would work better like this:
COMMAND=DEVP
...
let PROCS=$(UNIX95= ps -C $COMMAND | wc -l)-1
if test $PROCS -gt 0
then
...process is up
else
...process is down
fi
And with all scripts, always test the script by running it in trace mode:
sh -x ./myscript
Now you'll see all the steps and the results.
Bill Hassell, sysadmin
PROCS=`ps -ef|grep -v grep|grep ${COMMAND}|wc -l`
has a basic problem. ps and grep are seldom reliable. While $COMMAND is set to "DEVP", the above statement can return several matches because grep never looks at the process name...it looks at the entire line. The grep will match DDEVP, /opt/DEVPDIR/prog and many other strings, whether it is a program name or a parameter. ps is so much more powerful than most admins know. Using ps -ef is a big load on a busy server so let's have ps do the searching in the process table. Change ps to do the searches with 100% accuracy:
COMMAND=DEVP
UNIX95= ps -C $COMMAND
Now ps always returns EXACTLY the number of processes that have a basename of DEVP. The UNIX95= construct is needed to enable 3 useful options: -C -H -o. ps always supplies a header as line #1, so we have to remember that wc -l is 1 higher than needed.
Throughout the script, the construct $(...) is used for "Command Substitution" except in the PROCS= statement. $(...) is always preferred, so the code would work better like this:
COMMAND=DEVP
...
let PROCS=$(UNIX95= ps -C $COMMAND | wc -l)-1
if test $PROCS -gt 0
then
...process is up
else
...process is down
fi
And with all scripts, always test the script by running it in trace mode:
sh -x ./myscript
Now you'll see all the steps and the results.
Bill Hassell, sysadmin
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2025 Hewlett Packard Enterprise Development LP