Solved

bug in script

Posted on 2006-11-17
11
251 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
ID: 17969575
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
ID: 17969914
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
ID: 17970343
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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 14

Assisted Solution

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

Assisted Solution

by:ravenpl
ravenpl earned 295 total points
ID: 17970671
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
 
LVL 3

Author Comment

by:bryanlloydharris
ID: 17973110
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
ID: 17973129
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
ID: 17973865
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 40

Assisted Solution

by:noci
noci earned 41 total points
ID: 17999823
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
ID: 18000704
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
ID: 18012450
Thanks for all the help.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Network Interface Card (NIC) bonding, also known as link aggregation, NIC teaming and trunking, is an important concept to understand and implement in any environment where high availability is of concern. Using this feature, a server administrator …
I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

778 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