Link to home
Start Free TrialLog in
Avatar of bryanlloydharris
bryanlloydharris

asked on

bug in script

I am trying to write a script that can tell me if apache is not running, but it's not working.  Can anyone tell me, what am I doing wrong?

#!/bin/bash

tmpfile=$(mktemp)
pgrep -l apache | head -1 > $tmpfile
grep -q apache $tmpfile
[ $? -ne 0 ] && echo apache not running
rm $tmpfile
SOLUTION
Avatar of Cyclops3590
Cyclops3590
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bryanlloydharris
bryanlloydharris

ASKER

Very good stuff, it's just that I'll have to think a while to finally understand how it works.  Mine seems more bloated, yes, but I have an easier time understanding a simple one such as mine.

Both are much shorter than mine; I'll definitely end up using one of them and split the points.  Thanks for the very interesting answers.

Cyclops, I didn't previously know I could use "$result" = "", would that be the same as "$result" -eq ""?

I admit my fault: I really like the mktemp although I'm not making the most appropriate use of it.

Out of my own curiosity, can you guys tell me why mine _didn't_ work?  I was hoping someone could tell me what I did wrong.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I'm sorry but none of those worked for me.  I'm not sure why, but some of the above scripts worked from the command line but they didn't work from within a bash script.  Then, some worked from within a bash script but not from a bash script called from crontab.

So, in the end I realized I could just try and start apache over and over every minute.  If it's already running then nothing happens except an error.  If it's not running it starts.

* * * * * /usr/sbin/apache2ctl startssl

e.g.
# /usr/sbin/apache2ctl startssl
httpd (pid 13224) already running
Has anyone experienced something like this working from command line but not from script?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all the help.