Solved

bug in script

Posted on 2006-11-17
11
247 Views
Last Modified: 2013-12-16
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
0
Comment
Question by:bryanlloydharris
11 Comments
 
LVL 25

Assisted Solution

by:Cyclops3590
Cyclops3590 earned 41 total points
Comment Utility
any reason something like this wouldn't work

#!/bin/sh

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

if [ "$result" = "" ]
then
  echo "Apache not running"
else
  echo "Apache is running"
fi
0
 
LVL 2

Assisted Solution

by:jianmcgowan
jianmcgowan earned 41 total points
Comment Utility
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

#!/bin/bash
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.

#!/bin/bash
pgrep $1 > /dev/null || echo "$1 not running"

Hope this helps.
0
 
LVL 3

Author Comment

by:bryanlloydharris
Comment Utility
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.
0
 
LVL 14

Assisted Solution

by:ygoutham
ygoutham earned 41 total points
Comment Utility
= is used as a assignment of number and -eq for string
0
 
LVL 43

Assisted Solution

by:ravenpl
ravenpl earned 295 total points
Comment Utility
No need for temp files, and on my server I have to grep for httpd instead of apache

#!/bin/bash
pgrep httpd >/dev/null 2>&1 && echo "Apache is running" || echo "Apache is down"
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 3

Author Comment

by:bryanlloydharris
Comment Utility
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
0
 
LVL 3

Author Comment

by:bryanlloydharris
Comment Utility
Has anyone experienced something like this working from command line but not from script?
0
 
LVL 43

Accepted Solution

by:
ravenpl earned 295 total points
Comment Utility
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
0
 
LVL 39

Assisted Solution

by:noci
noci earned 41 total points
Comment Utility
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.
0
 
LVL 8

Assisted Solution

by:jk2001
jk2001 earned 41 total points
Comment Utility
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.
0
 
LVL 3

Author Comment

by:bryanlloydharris
Comment Utility
Thanks for all the help.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now