Problem with looping construct

Hi all

I have tried to search for this, but keep getting a MySQL db connect error, so am posing the question here, and taking a risk of incurring the wrath of the mods with my first post...

I have the following test script:

#!/bin/bash

HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd >/dev/null; echo $?`

        while [ ${HTTPD} -eq 0 ]
        do
                echo "Apache is still running..." 
        done

echo "Apache has stoppped..."
echo "exiting..."

The final idea is to run the while loop until the state of httpd changes from 0 to 1 (i.e. from running to stopped). However, when I set this script to run, the output to stdout is obviously "Apache is still running...". Yet when I halt Apache with the command "sudo apachectl stop", the output to stdout is still "Apache is still running...", which quite clearly, is not true.

This is running on Mac OS 10.4.7 Server on a dual PPC Xserve

Anyone got a clue as to what I am doing wrong?

Mike

OK, first of all, you should have an if condition there and not a while. Running this script will give you an infinite number of "Apache is still running..." outputs because the script only checks for apache running at the beginning and not in every iteration of the while loop. If you do want to use while, run the ps command inside the while loop.

Secondly, sleep. Use the sleep command so that your script does not hog system resources by constant condition checking.

Since you are using bash, you could rewrite the script like this:

while [ $(/usr/ucb/ps -axcu | /usr/bin/grep httpd > /dev/null; echo $?) -eq 0 ]; do 
   echo "running"; 
   sleep 10; 
done

Sleep for longer though...

Hi Blowtorch

Thanks for the reply, and the input. I used the method you suggested - running the "ps" inside the while loop - and it worked. What I don't understand is why this did not work in my original script as within the while loop, there is the call for the command substitution:

HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd >/dev/null; echo $?`

while [ ${HTTPD} -eq 0 ];
do
echo "Apache is still running..."
done

Prehaps this is highlighting gaping holes in my knowledge :slight_smile:

Mike

this command executed only once

HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd >/dev/null; echo $?`

because it is out of while loop.And in while loop you are checking status.which will be same for infinite times while executes.Sleep 60 sounds good to me here.

Ok, you have a couple of misunderstandings there.

  1. You are trying to define HTTPD variable as a command, but you are using the ` (backtick) character to do that. Using backticks actually causes the command to execute and the results to be returned and (in this case) stored in the HTTPD variable. Use the ' (single quote) character to do what you want.
  2. You want to run the command line contained under the HTTPD variable (forget that you aren't getting the command line for a moment), but you are using ${} instead of $(). The $() is used to fork a subshell and execute processes. ${} is used to address shell variables.

By the way, I tried writing and running the script the way you want to, but it is giving me an error.

# cat test.sh
#!/usr/bin/bash

HTTPD='/usr/ucb/ps -axcu | /usr/bin/grep httpd > /dev/null; echo $?'
while [ $($HTTPD) -eq 0 ]; do
        echo "running"; sleep 10; 
done
# bash -x test.sh
+ HTTPD=/usr/ucb/ps -axcu | /usr/bin/grep httpd > /dev/null; echo $?
++ /usr/ucb/ps -axcu '|' /usr/bin/grep httpd '>' '/dev/null;' echo '$?'
ps: too many arguments
usage: ps [ -aceglnrSuUvwx ] [ -t term ] [ num ]
+ '[' -eq 0 ']'
test.sh: [: -eq: unary operator expected

I don't understand what the shell is passing as arguments to the ps command in this case. I tried escaping the |, the > and the $ in $?, but that didn't work. Maybe someone else would like to have a go.

Hi there

@Dhruva

Thank you for your reply; I think I understand what you are explaining to me, and I have added a "sleep 60" :slight_smile:

@Blowtorch

Thank you for the information on the backtick. I honestly thought that ` and $() were the same, and as you point out, they do different things which makes it a bit clearer now :slight_smile:

Regarding the "ps" command:

Using the flag "-a", I am able to view other users processes as well as mine (and as I am after httpd/Apache, I need this function).
Using the flag "-c", I only get the command that is being run by the user, and not the full path to that command.
Using the flag "-u", means that I get information regarding the users running services, amongst other things.
Using the flag "-x", displays all processess, including those without controlling terminal sessions. I need this one to display the "httpd" process, in this case, run as the user "www".

To be honest, typing "ps -axcu" is force-of-habit every time I wish to view the process list, so that is probably the main reason why it is like that in the script.

I am running "ps" under a BSD flavour (in this case, Mac OS X), so your "ps" might be different. For example, your location for "ps" would appear to be "/usr/ucb/ps", whereas mine is "/bin/ps". Again, "ifconfig" under GNU/Linux flavour Ubuntu, does not contain the options "alias" or "-alias", for, well, aliasing IP's and netmasks to one physical network interface, whereas in the Mac OS X version, it does.

Does that answer your query at all?

This script is one in a series that I am writing at the moment, as IP Failover in OS X is quite limited, in that the heartbeat daemon can not be attached to any services (kind of silly), so the only way the master will failover to the backup server is if the heartbeat fails. In this instace, the heartbeat will only fail if the interfaces that it is sent over, go down. The flaw in this is that a service such as Apache of PostgreSQL can fail, but the interfaces will remain active, and therefore no failover.

Mike

Maybe it is because in your cat'd file, the command assigned to the variable HTTPD is not enclosed by backticks? When I run it as you have it displayed, I get the following error:

$ sh test.sh 
test.sh: line 5: [: too many arguments

Mike