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
#!/bin/bash
tmpfile=$(mktemp)
pgrep -l apache | head -1 > $tmpfile
grep -q apache $tmpfile
[ $? -ne 0 ] && echo apache not running
rm $tmpfile
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
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
ASKER
Has anyone experienced something like this working from command line but not from script?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for all the help.
ASKER
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.