• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 271
  • Last Modified:

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?


pgrep -l apache | head -1 > $tmpfile
grep -q apache $tmpfile
[ $? -ne 0 ] && echo apache not running
rm $tmpfile
7 Solutions
any reason something like this wouldn't work


result=`ps aux | grep httpd | grep -v grep`

if [ "$result" = "" ]
  echo "Apache not running"
  echo "Apache is running"
This script works for me running ubuntu dapper.  A suggestion for general bash debugging and a comment:

1) Add -x to the first line to trace execution of the bash script.  I.e. "#!/bin/bash -x"
2) There's no reason to have a temp file, though your use of mktemp is the right way to do so.  Try

pgrep -l $1 | head -1 | grep -q apache || echo "$1 not running"

And then "testrunning apache" returns nothing, while "testrunning qwerty" says that qwerty is not running.

More minimally, you can test the results of pgrep directly.

pgrep $1 > /dev/null || echo "$1 not running"

Hope this helps.
bryanlloydharrisAuthor Commented:
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.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

= is used as a assignment of number and -eq for string
No need for temp files, and on my server I have to grep for httpd instead of apache

pgrep httpd >/dev/null 2>&1 && echo "Apache is running" || echo "Apache is down"
bryanlloydharrisAuthor Commented:
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

# /usr/sbin/apache2ctl startssl
httpd (pid 13224) already running
bryanlloydharrisAuthor Commented:
Has anyone experienced something like this working from command line but not from script?
No, and in fact I have script, which tries many services. For apache mine looks like
ps -C httpd >/dev/null 2>&1 || /etc/init.d/httpd restart
nociSoftware EngineerCommented:
stuff not working under cron motly comes from $PATH expectation that are not met.
cron doesn't run scripts like .bashrc, .profile etc. to setup a path or aliases
for commands. ==> you only have a very basic environment when running from cron.

Again with the -x added to the line you should get a mail from the cron daemon
that explains what goes wrong.
John KawakamiCommented:
Read up on apachectl, and read the sources for /etc/init.d/apache or /etc/init.d/httpd.  The latter does what you want, so you might just copy the code from there.
bryanlloydharrisAuthor Commented:
Thanks for all the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now