![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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..."
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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 |
|
#3
|
|||
|
|||
|
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
Mike |
|
#4
|
||||
|
||||
|
this command executed only once
Code:
HTTPD=`/bin/ps -axcu | /usr/bin/grep httpd >/dev/null; echo $?` |
|
#5
|
||||
|
||||
|
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
|
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
Quote:
Code:
$ sh test.sh test.sh: line 5: [: too many arguments |
|||
| Google The UNIX and Linux Forums |