Problem with looping construct


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with looping construct
# 1  
Old 09-07-2006
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:
Code:
#!/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
# 2  
Old 09-07-2006
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:
Code:
while [ $(/usr/ucb/ps -axcu | /usr/bin/grep httpd > /dev/null; echo $?) -eq 0 ]; do 
   echo "running"; 
   sleep 10; 
done

Sleep for longer though...
# 3  
Old 09-07-2006
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:

Code:
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 Smilie

Mike
# 4  
Old 09-07-2006
this command executed only once

Code:
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.
# 5  
Old 09-07-2006
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.
Code:
# 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.
# 6  
Old 09-07-2006
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" Smilie

@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 Smilie

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
# 7  
Old 09-07-2006
Quote:
Originally Posted by blowtorch

Code:
# 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.
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:

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

Mike
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Looping problem

I need help. I am trying to get this script to send out only one email not multiple emails of the abend. Currently it will send me one then and ther with the first and second one then another email with the first second and third abend and so on. I only want one email sent. ... (2 Replies)
Discussion started by: bbcarosi
2 Replies

2. Shell Programming and Scripting

Problem with Looping

Hi, guys, What I want is exactly shown below (I modified the former image and it looks like clearer.) https://lh6.googleusercontent.com/-EG8SKkrWEvc/Ube9e-jDiHI/AAAAAAAAAOM/hFNT0UqQPWE/s512/Linux_Study_20130611_001.jpg And with some guys' help, I made it. My script is below: #!/bin/bash #... (20 Replies)
Discussion started by: franksunnn
20 Replies

3. Shell Programming and Scripting

perl: problem in looping! how to get rid

Hi i just want to open 2 files and find difference between cond1 and cond2 and if the difference is greater than or equal to some number say 2 print the lines again in 2 different files. file 1 (1.txt) aqw dfr 34 poilo ggg 98 file 2 (2.txt) qww asd 28 poilo ggg 97 open FILE1,"1.txt" or... (2 Replies)
Discussion started by: anurupa777
2 Replies

4. Shell Programming and Scripting

looping problem

I have been trying to come up with a program that can do this: Say I have a file named "sir" with a single field; 10 229 288 35 83 47 3 I want to create a file "gen" with three fields with the data in file "sire" listed in field 1 while field 2 and 3 are just 1 each like this: SPARSE... (1 Reply)
Discussion started by: iconig
1 Replies

5. Shell Programming and Scripting

Help with if-else construct

Hi all i have been trying to do a small 'question and answer' script using if-else statement and a combination of pipe. I have succeeded in allowing the user to login with user name and password stored in a sequence username/password in a file named "pass" like this: echo "please enter your... (14 Replies)
Discussion started by: arikutex
14 Replies

6. Solaris

SVM Solaris 8 Problem. Metastat output looping

Hi friends, I'm newbie to SVM. Just wanna try installed it on one of our server (to do mirroring for disk0 and disk1) but i think im lost until now. :( the steps i've taken is as below:- 1.prtvtoc /dev/rdsk/c1t0d0s2 | fmthard -s - /dev/rdsk/c1t1d0s2 2.metadb -a -c 3 -f c1t0d0s7... (3 Replies)
Discussion started by: kronenose
3 Replies

7. Shell Programming and Scripting

if-else construct not working

Hi all, Sorry to ask this easy question but I am stuck. In a scenario i am executing one shell script which contains a if - else construct : if ; then echo $line $line >> successful_build.txt else $line >> failed_services.txt fi explaination : if the... (5 Replies)
Discussion started by: bhaskar_m
5 Replies

8. Shell Programming and Scripting

Problem with looping the directories

Hi all, I have a directory which has many sub-directories. Now, I want to check the space of each dir and redirect the output in a file if space exceeds the limit. I have already done it, but the way I did is not very good. I just listed the directories and awked the last column to get the... (5 Replies)
Discussion started by: naw_deepak
5 Replies

9. Shell Programming and Scripting

ksh construct

Hi Guys, could someone tell me what this ksh construct does typeset -r PROG_PWD=${0%/*} does I understand the -r for readonly but I would very much appreciate a definitive account of what this will set $PROG_PWD to. If I run this at the cmd line it it gets set to /usr/bin but I would... (2 Replies)
Discussion started by: ajcannon
2 Replies

10. Shell Programming and Scripting

Awk: looping problem!

I am having a problem with awk when I run it with a loop. It works perfectly when I echo a single line from the commandline. For example: echo 'MFG009 9153852832' | awk '$2 ~ /^0-9]$/{print $2}' The Awk command above will print field 2 if field 2 matches 10 digits, but when I run the loop... (5 Replies)
Discussion started by: cstovall
5 Replies
Login or Register to Ask a Question