On Fri, Jan 30, 2004 at 02:06:26PM +0000, Jim Hodgson wrote:
> I'm new to Linux and cron. The ldap and dhcp randomly shut down. I want to 
> learn how to write a cron to check that they are running and if not start 
> them. I understand the first part of the cron, time, day, date, etc. But 
> not knowing enough about linux, I don't know what to write for the command 
> part.

You need to provide a command to execute.  That can either be an existing
command, or a script which has a set of commands to execute (like a batch file
or windows script if you're used to those sorts of things).

I'd suggest that you write one of these scripts to do the necessary checking.
Call it perhaps /usr/local/sbin/service_check, and put something like this into
it:

#!/bin/sh

# First get the PID of the service (LDAP as an example)
PID=`cat /var/run/slapd.pid`

# Now get the process list, and look for the given process ID
PROCESS=`ps ax | grep "^ *$PID"`

if [ -z "$PROCESS" ]; then
        /etc/init.d/slapd restart
fi

# Here endeth the script

(Note to the gurus watching - yes, I know this can be made a lot more terse,
but we're not working for Confuse A Cat, we're explaining things).

If you're not up on how bourne shell scripting works, that's likely to be
complete double dutch.  Welcome to Unix!  Once you get into it, though, you'll
find it is incredibly expressive and powerful.

A couple of things to note - I'm assuming that the LDAP server's PID is in
/var/run/slapd.pid (it's been so long since I've had to look, and I don't have
an LDAP server particularly handy to check).  If you're running an
RPM-based distribution, you'll probably need to change the restart command.
You'll probably need to make the script executable after writing it, with
chmod 0755 /usr/local/sbin/service_check.  Oh, and that script is totally
untested - I don't *think* it'll make your cat pregnant, but there's no
warranty on that.  However, if the kittens are particularly cute, I might take
one.

Now you put your entry in your crontab, pointing to
/usr/local/sbin/service_check as the command, and you're away.

Of course, the better way to solve this particular problem would be to just
work out why your services are dying, and fix that...

- Matt
-- 
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to