The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help on looping using if/for or while sam4now Shell Programming and Scripting 1 04-24-2008 10:33 AM
ksh construct ajcannon Shell Programming and Scripting 2 09-27-2007 06:08 AM
Embedding Perl construct in ksh... svetlur Shell Programming and Scripting 1 06-07-2007 06:39 PM
Awk: looping problem! cstovall Shell Programming and Scripting 5 06-09-2005 07:36 AM
extracting info from Unix database to construct a visual diagram fusion99 UNIX for Advanced & Expert Users 0 11-29-2004 10:29 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-07-2006
Registered User
 

Join Date: Sep 2006
Posts: 16
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
Reply With Quote
Forum Sponsor
  #2  
Old 09-07-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
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...
Reply With Quote
  #3  
Old 09-07-2006
Registered User
 

Join Date: Sep 2006
Posts: 16
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

Mike
Reply With Quote
  #4  
Old 09-07-2006
Dhruva's Avatar
Registered User
 

Join Date: Mar 2006
Location: India
Posts: 255
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.
Reply With Quote
  #5  
Old 09-07-2006
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,326
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.
Reply With Quote
  #6  
Old 09-07-2006
Registered User
 

Join Date: Sep 2006
Posts: 16
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"

@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

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
Reply With Quote
  #7  
Old 09-07-2006
Registered User
 

Join Date: Sep 2006
Posts: 16
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
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
linux, ubuntu

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:13 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0