LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 10-22-2008, 02:27 PM   #1
ABL
Member
 
Registered: Mar 2005
Location: NYC
Distribution: CentOS 5
Posts: 54

Rep: Reputation: 16
check and restart (if dead) a daemonized process


I have been using Unison for a year or two to replicate our file server between remote locations (both running CentOS) in near-real time, and it works extremely well.


I have also set up a cron job to make sure that a daemonized version of Unison is running and to restart it, if it's not.

Unfortunately, my script doesn't seem to work consistently.

I have placed the following script in /etc/init.d, so that Unison starts upon machine startup:
unisond:

#! /bin/bash
#
# unisond Start/Stop Unison.
#
# Script created by: Alden Levy, Engine No. 9, Inc.
#
/usr/local/sbin/daemonize -a -e /var/log/unison/error.log -o /var/log/unison/unison.log -p /var/run/unison.pid -v /usr/bin/unison


And in /etc/cron.hourly, I created
check_unison:
#! /bin/bash
#
# check_unison Check to see if Unison is running, and restart it, if it's not.
#
# Script created by: Alden Levy, Engine No. 9, Inc.
#
export upid=$(pidof unison)
export ustorepid=$(cat /var/run/unison.pid)
if [ "$upid" != "$ustorepid" ] || [ -z "$upid" ] #if the PID of unison != stored PID of unison then
#unison is not running, or was started incorrectly
if [ -n "$upid" ]; then
kill "$upid" #if unison is running, it wasn't running correctly, so kill it
fi
if [ -f /var/run/unison.pid ]
then
rm -f /var/run/unison.pid #remove stored PID of unison
fi
/etc/init.d/unison & #start unison
pidof unison > /var/run/unison.pid #store PID of unison
exit 1 #exit with code 1 to show that unison was restarted
else
echo "unison is running" #otherwise, we're cool, so tell us and exit
exit 0
fi
exit 0


My scripts run and create the pid file. Permissions are 755 and owned by root:root. If I run check_unison, it works okay, but every few months, I see that Unison has stopped, and it won't restart without my intervention.

Any help you can provide would be greatly appreciated.

Thanks!
 
Old 10-22-2008, 07:36 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
Personally I'd put a short sleep (10 seconds) between these 2 lines:

/etc/init.d/unison & #start unison
pidof unison > /var/run/unison.pid #store PID of unison

because it may not start successfully, ie it may start, but then fail almost immediately. Because you're not waiting at all, you may catch that initial start, so think its ok, but it could fail a few seconds later.
Also, some progs take a few seconds to startup, especially if the system is busy.
If it doesn't already log everything to eg /var/log/messages, at startup
/etc/init.d/unison
the I'd add

/etc/init.d/unison >/var/log/unison.log 2>&1 &

or similar.
 
Old 10-23-2008, 12:48 PM   #3
ABL
Member
 
Registered: Mar 2005
Location: NYC
Distribution: CentOS 5
Posts: 54

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by chrism01 View Post
Personally I'd put a short sleep (10 seconds) between these 2 lines:

/etc/init.d/unison & #start unison
pidof unison > /var/run/unison.pid #store PID of unison

because it may not start successfully, ie it may start, but then fail almost immediately. Because you're not waiting at all, you may catch that initial start, so think its ok, but it could fail a few seconds later.
Also, some progs take a few seconds to startup, especially if the system is busy.
If it doesn't already log everything to eg /var/log/messages, at startup
/etc/init.d/unison
the I'd add

/etc/init.d/unison >/var/log/unison.log 2>&1 &

or similar.
Thanks. I'll give it a try and report back.
 
Old 10-23-2008, 01:19 PM   #4
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by ABL
I have also set up a cron job to make sure that a daemonized version of Unison is running and to restart it, if it's not.

Unfortunately, my script doesn't seem to work consistently.
Not to marginalize your own work at this endeavor, but check out daemontools if you'd like to try an alternative to your homegrown solution.

http://cr.yp.to/daemontools.html

Quote:
supervise monitors a service. It starts the service and restarts the service if it dies. Setting up a new service is easy: all supervise needs is a directory with a run script that runs the service.
 
Old 10-23-2008, 01:50 PM   #5
ABL
Member
 
Registered: Mar 2005
Location: NYC
Distribution: CentOS 5
Posts: 54

Original Poster
Rep: Reputation: 16
Quote:
Originally Posted by anomie View Post
Not to marginalize your own work at this endeavor, but check out daemontools if you'd like to try an alternative to your homegrown solution.

http://cr.yp.to/daemontools.html
It's funny, but I remember looking at this a year or two ago, but I can't remember why I didn't use it.

I'll give it a try. Thanks for the idea.
 
  


Reply

Tags
restart, script, unison



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
Bash script to check for dead process MaffooClock Other *NIX 12 05-18-2007 05:14 AM
need help to write script to check the process health and automatically restart it dragondad Programming 3 11-01-2006 12:23 PM
Crond Dead But Subsys Locked, cannot restart cron services jmm8142 Linux - Software 4 05-23-2005 12:41 PM
script to check if process is dead or running rspurlock *BSD 6 04-12-2004 11:32 PM
cron job to restart process if dead gborrageiro Linux - General 3 09-25-2002 11:55 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:54 AM.

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