Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I know I have to count how many instances are running:

ps x | grep apache2 | wc -l 

result if it's running: 2, or else: 1

I also know there is a command called test that I could use to perform the verification, but I don't know how to use test with wc

share|improve this question
    
W​h​ich distro? –  Ignacio Vazquez-Abrams Apr 16 '10 at 13:56
1  
"/sbin/service apache2 status" will show if the service is start or stop. –  Chau Chee Yang Apr 16 '10 at 14:02
    
@Ignacio ubuntu –  Jader Dias Apr 18 '10 at 14:11
add comment

3 Answers

up vote 4 down vote accepted

The startup scripts on most Linux distributions handle this, by saving the Process ID of the service (daemon) when they launch it.

These PIDs are typically stored in /var/run/. For example on my system Apache2 stores it original or parent process ID (useful for pre-forked processes) in /var/run/apache2.pid. So you can then send signals such as SIGHUP or SIGSTOP to the process to signal either a configuration reload or to stop the service (daemon).

One note to help with your ps ax style process searching is to use a little trick to escape the grep's regular expression pattern used for matching, such that it doesn't match itself. E.g. ps ax | grep [a]pache2 | wc -l

man test gives you the basics of the test command. For simple tasks in shell scripting, it best to avoid being dependent on which shell you are using. Normally for basic scripting, writing scripts targeting the Borne Shell (/bin/sh) is recommended, as this tends to be the most popular lowest common denominator.

#!/bin/sh

RC= `ps ax | grep [a]pache2 | wc -l`

if [ $RC -gt 0 ]; then
  kill -SIGSTOP `cat /var/run/apache2.pid`
fi
share|improve this answer
    
What a clever idea to use grep [a]pache2! Thanks for that. –  n.st Jul 21 '13 at 13:05
add comment

If you are working with bash, you can do it with conditional checks:

#/bin/bash
num="1"
if [ $num -eq "1" ]; then
        echo "got 1"
fi

if [ $num -eq "2" ]; then
        echo "got 2"
fi
share|improve this answer
    
how to make num equal to ps x | grep apache2 | wc -l ? –  Jader Dias Apr 16 '10 at 13:45
    
@Jader, I've included an example in my answer. –  mctylr Apr 16 '10 at 14:22
add comment
if (( $(ps x | grep [a]pache2 | wc -l) > 0 ))
then
    do_something
else
    do_something_else
fi

or

case $(ps x | grep [a]pache2 | wc -l) in
    0)
        do_zero_thing
        ;;
    1)
        do_one_thing
        ;;
    *)
        do_default_thing
        ;;
esac

The square brackets used in that way in the grep command suppresses the grep itself from being included in its own output. This will make the process count 0 if apache isn't running.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.