Computing.Net > Forums > Unix > write log in loop - ksh script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

write log in loop - ksh script

Reply to Message Icon

Name: anhvu00
Date: March 13, 2003 at 09:33:46 Pacific
OS: Solaris
CPU/Ram: pentium 512
Comment:

Hello,
I have a script to start 2 servers and keep a log of when they are down/restarted.
The problem is in writing the log. It only writes once. May be the OS doesn't check the loop condition anymore when it recognizes it is an infinite loop? Please help me to see what I did wrong. Thank you very much.
Anh
-----------

#!/bin/ksh
# Author: Anh Vu
# Version: 1.1
# Date: 3/7/03
# 1. check for server1 and server2 process currently running
# 2. if server1 is not running, check if server2 is running
# 3. if so, kill server2 and start server1 then server2
# 4. if server2 is not running restart it


# define control log path
LOGFILE="./control.log"

#===================================================================================
s_now() # Returns string "YYYYMMDD-HH:MM:SS"
#===================================================================================
{
date +%Y%m%d-%H:%M:%S
}

# start a log for this script
echo "Control log starts " $(s_now) > ${LOGFILE}

while true
do
pid1=`ps -e | grep hello | awk '{print $1}`
if [ "${pid1}" -eq "" ]
then
# server1 is not running
echo "server1 stops " $(s_now) >> ${LOGFILE}
# check for if server2 is running
if [ "${pid2}" != "" ]
then
# server2 is still up, terminate it
echo "Terminate server2 to restart server1 " >> ${LOGFILE}
kill -9 ${pid2}
echo "server1 starts " $(s_now) >> ${LOGFILE}
nohup hello & ulimit -f 0
fi
fi

# see if server2 is running
pid2=`ps -e | grep bye | awk '{print $1}`
if [ "${pid2}" -eq "" ]
then
# server2 is not running
echo "server2 stops " $(s_now) >> ${LOGFILE}
nohup bye & ulimit -f 0
echo "server2 starts " $(s_now) >> ${LOGFILE}
fi
done



Sponsored Link
Ads by Google

Response Number 1
Name: Don Arnett
Date: March 13, 2003 at 13:00:49 Pacific
Reply:

Where is 'true' defined. I've never seen that defined in shell scripting.

Try either:

true=1
while $true


or


while 1



0

Response Number 2
Name: WilliamRobertson
Date: March 13, 2003 at 14:53:07 Pacific
Reply:

"true" is standard ksh, and means the same as ":"

nohup hello & ulimit -f 0
has me puzzled though. You nohup "hello" (= start your server process?) in background, but where does "ulimit -f 0" come in?

Which of the writes to $LOGFILE is not working? Maybe a conditional test is not giving the results you expect.


0

Response Number 3
Name: anhvu00
Date: March 15, 2003 at 11:59:57 Pacific
Reply:

Dear Don & William,

- My appology for the sloppy code. I had a version of
true=1
while [ ${true} -eq 1 ]...
They both work the same way.

- The "ulimit -f 0" after "nohup" forces the nohup output file size to be 0. The reason behind is to save disk space for the log files.

- The "hello" and "bye" processes are small C programs (for this testing purposes) that do something like:
void main() {
int i;
for (i=0; i20; i++} {
printf("hello\n);
sleep(1);
}
}
- The output is like the loop only runs once.
For example:
control log starts datetime
server1 stops datetime
server1 starts datetime
server2 stops datetime
server2 starts datetime

After 20 seconds, when "hello" exits and restarts again (the script indeed restarts the "hello" and "bye" process.). I do not see the new information written in the control log.

Thank you very much for your time.
Sincerely,
Anh


0

Response Number 4
Name: WilliamRobertson
Date: March 16, 2003 at 04:45:38 Pacific
Reply:

Hey Anh, not sloppy code at all! I prefer the built-in true/false (or you could use something like "while [[ 1 = 1 ]]"). I would personally standardize on either Bourne style `` or Korn style $() (likewise Bourne [] vs Korn [[]]), though they are both fine in your example. (I prefer $() because it nests better.)

Not sure if this is it, but "ps -e | grep hello" may always find something because the "grep" command line itself contains "hello", therefore the first "if" condition is never met. You could try "ps -e | grep hello | grep -v grep" (there is probably a neater solution using awk to check specific fields).


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: write log in loop - ksh script

Unix-ksh script www.computing.net/answers/unix/unixksh-script/6758.html

ksh scripting - numeric vs. alpha www.computing.net/answers/unix/ksh-scripting-numeric-vs-alpha/4549.html

Waiting problem - KSH script www.computing.net/answers/unix/waiting-problem-ksh-script/7835.html