From owner-freebsd-questions@FreeBSD.ORG Sun Feb 13 19:54:24 2005 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 0F8E416A4CE for ; Sun, 13 Feb 2005 19:54:24 +0000 (GMT) Received: from smtp813.mail.sc5.yahoo.com (smtp813.mail.sc5.yahoo.com [66.163.170.83]) by mx1.FreeBSD.org (Postfix) with SMTP id D15E343D39 for ; Sun, 13 Feb 2005 19:54:23 +0000 (GMT) (envelope-from pauls@utdallas.edu) Received: from unknown (HELO officeeagle) (pschmehl@sbcglobal.net@66.140.63.203 with login) by smtp813.mail.sc5.yahoo.com with SMTP; 13 Feb 2005 19:54:23 -0000 Message-ID: <042c01c51205$d0429c90$7702a8c0@officeeagle> From: "Paul Schmehl" To: =?iso-8859-1?B?S/Z2ZXNk4W4gR+Fib3I=?= , References: <420FA6F5.1020609@t-hosting.hu> Date: Sun, 13 Feb 2005 13:54:03 -0600 MIME-Version: 1.0 Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=response Content-Transfer-Encoding: 8bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2900.2527 x-mimeole: Produced By Microsoft MimeOLE V6.00.2900.2527 Subject: Re: Crontab script X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 13 Feb 2005 19:54:24 -0000 ----- Original Message ----- From: "Kövesdán Gábor" To: Sent: Sunday, February 13, 2005 1:13 PM Subject: Crontab script > Hi, > > I've seen somewhere an easy way to check whether a program with a > specified pid is running or not. I've made a crontab script to check my > programs based on this. The script is the following: > > #!/bin/sh > PID_FILE="/usr/local/bopm/var/bopm.pid" > PID=`cat $PID_FILE` > EXECUTABLE="/usr/local/bopm/bin/bopm" > Check for a pid file is not a good way to see if a program is running. *Sometimes* they will be running even though there is no pid file (even though there's supposed to be one.) This would be bettter: either ps -auxw | grep {program name} | awk {'print $2'} or pgrep {program name} (pgrep is available on the web) If you chose the former, you may have to put in a second grep to eliminate "finding" your own command. Something like this: ps -auxw | grep {program name} | grep {commandline switch of the program} | awk {'print $2'} You will want to test this on the commandline first to make sure you're getting the right process. Putting this all together then, with a specific example that I know about: #!/bin/sh APACHE=/usr/local/sbin/apachectl PID=`ps -auxw | grep httpd | grep "\-DSSL" | grep root | awk {'print $2'}` DATE=`date +"%m-%d-%H:%M:%S" LOG=`tail /var/log/httpd-error.log` if [ ! -z $PID ]; then $APACHE start echo "Restarted apache at $DATE" echo $LOG fi This will check to see if it's running, and if it's not, start it and send you the date/time it was started and the last 10 lines of the error log. Since you're running it in cron, you'll get email with the output. If you wanted, you could redirect stderr to a log to see if there were any problems. Paul Schmehl (pauls@utdallas.edu) Adjunct Information Security Officer The University of Texas at Dallas AVIEN Founding Member http://www.utdallas.edu/~pauls/