LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-04-2003, 01:55 PM   #1
lwsivad
LQ Newbie
 
Registered: Nov 2003
Posts: 5

Rep: Reputation: 0
Script to check daemon


I have several different daemons running on a server. I want to write a sure fire script for cron to make sure the daemon is still running (every 10 minutes). I am getting close but I can't figure out how to finish the script. I tried checking for the existance of a pid file but it is there and the daemon isn't running.

I can check manually check for it several ways (ps or netstat). Below is the general syntax I'm trying:

if [ ! netstat -nl | grep :137 ]; then
/usr/local/samba/sbin/nmbd
fi

It is checking to see if the server is listening on any port 137. If it isn't then the [ ] is null and should execute the line below it. I keep getting an error:
./check.samba: [: missing `]'
grep: ]: No such file or directory

I tried to do this also: `netstat -nl | grep :137` but I get different errors. It seems I just don't know what syntax to use.

Any help??
 
Old 11-04-2003, 02:12 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Google/see Freshmeat for "monit". Much easier, dependable.
And if you don't want Monit to die, start it from init (after appropriate testing).
 
Old 11-04-2003, 02:56 PM   #3
lwsivad
LQ Newbie
 
Registered: Nov 2003
Posts: 5

Original Poster
Rep: Reputation: 0
I found a temporary solution (maybe a permanent one). Now I have it in cron to check every 10 minutes to see if the daemonname is running. Here is the syntax I used:

if [ -z `ps -ef |grep daemonname |grep -v grep | awk '{print$2}'` ]; then
/path/to/start/daemon
fi

There were 2 things I was doing wrong: needed ` ` to enclose the expression and I was using -n which checks to see if an "expression exist AND if is NOT NULL". The -z checks to see if the "expression exist and is NULL". Those 2 parts were causing the errors. http://www.freeos.com/guides/lsst/ was very helpful in getting this done.

I plan on looking at monit to see if it does what I need without having to spend too much time getting a PHD in it.

Thanks for the help.
 
Old 11-04-2003, 03:08 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Yeah, I sometimes forget to address the actual question...
Here's a oneliner you can run from cron:
"pgrep <daemonname> 2>&1>/dev/null || /path/to/start/<daemonname>"
Note this doesn't work if crond goes down for any reason.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
daemon Shell Script abdul_zu Linux - Newbie 10 10-10-2005 10:13 AM
Any command to check Daemon acbenny Linux - Networking 1 07-29-2004 01:38 PM
Any Command to check Daemon acbenny Linux - General 1 07-29-2004 12:51 PM
Running script as daemon StinkyFinger Linux - Newbie 2 08-28-2003 07:31 PM
a daemon script robeb Linux - General 0 09-14-2002 03:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration