|
Author |
Message |
|
|
Posted Aug 04, 2008 at 10:07:37 PM
Subject: Setting up a shell script to check if a process is
Hi,
I am trying to write a simple shell script to run on a cron schedule to ensure that a given process is running and then to take an action based on that result. In this case I am attempting to look at /sbin/pidof to see if a valid value is present for a given process. I'm not sure if this is the best way to accomplish this so I'm open to other suggestions...
I have the following in a shell script:
if [ -z "/usr/sbin/pidof ntop" ] ;
then
echo "ntop is no longer running"
else
echo "ntop is running and nothing needs to be done"
fi
The -z is in place assuming that the command /usr/sbin/pidof ntop comes back as a null value (which I'm assuming it should if ntop is not actually running).
This is obviously very simple at this stage and I will change it to send an email and restart the service if ntop has stopped running but first I'd like to figure out why this doesn't work.
Thanks!
|
chart3399
Joined Jun 17, 2008 Posts: 73
Other Topics
|
Posted:
Aug 04, 2008 10:23:19 PM
Subject: Setting up a shell script to check if a process is
What you are looking to do is pretty simple. There is nothing wrong with doing it your way. I would do it a little differently but it is more up to style. What I typically try to do with all my scripts is think long term. Today you want to monitor ntop so hard coding ntop into the script isn't bad but it isn't very extensible either. Lets say tomorrow you want to monitor ntop and process foo? You see where I am going with this.
I typically work with exit values so I would have written this basic script as:
#!/bin/sh
/usr/sbin/pidof ntop > /dev/null 2&>1
if [ $? -gt 0 ] ; then
echo "We got a problem"
fi
If you want to impress your friends
#!/bin/sh
/usr/sbin/pidof ntop > /dev/null 2>&1 || echo "We got a problem"
|
AdrianTM
Joined Aug 17, 2008 Posts: 1
Location:USA
Other Topics
|
Posted:
Aug 17, 2008 4:22:35 PM
Subject: Setting up a shell script to check if a process is
I would use the name of the process since you want to find out if there's at least one instance running.
For example to check if sshd runs and if it doesn't to start it, use something like this:
ps -C sshd || "here goes the command to start sshd"
on my system it looks like this:
ps -C sshd || service ssh restart
[Modified by: AdrianTM on August 17, 2008 04:23 PM]
|
Fabio Berbert de Paula
Joined Aug 29, 2008 Posts: 7
Other Topics
|
Posted:
Sep 01, 2008 9:53:24 PM
Subject: Setting up a shell script to check if a process is
I have an alternative solution that works fine for me! It is based on the output off "ps" command. It works in looping and check the proccess every 5 seconds.
Here we go (sample for apache2):
---
#!/bin/sh
CMDNAME='apache2'
RESTARTCMD='sudo /etc/init.d/apache2 restart'
SLEEPTIME=5
while test 1
do
OUTPUT=`ps ax | grep $CMDNAME | grep -v grep`
if ! echo $OUTPUT | grep $CMDNAME 1>/dev/null ; then
echo Restarting service...
$RESTARTCMD
fi
sleep $SLEEPTIME
done
---
Fabio Berbert de Paula
http://www.vivaolinux.com.br/
|
Johannes Truschnigg
Joined Jun 15, 2008 Posts: 27
Other Topics
|
Posted:
Sep 03, 2008 2:54:52 PM
Subject: Setting up a shell script to check if a process is
If you're using a GNU/Linux based system with the procps tools installed (I don't think there is a non-embedded distro out there which hasn't), you you `pgrep` and `pkill` at hand. Read their manpages, and tap their power for your cause ;-)
Free software. Free society. Better lives.
|