Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

Where is 'true' defined. I've never seen that defined in shell scripting.
Try either:
true=1
while $true
or
while 1

"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.

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 datetimeAfter 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

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).

![]() |
![]() |
![]() |

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