Hi All, I want to write a bash script which can be executed by cron every minute to check if an application is running. If it is not running the script should start it again. What I normally would do to check if it is running is execute: # ps aux | grep App root 7479 15.4 0.7 21300 7816 pts/1 Sl 06:34 0:00 /usr/local/bin/App root 7495 0.0 0.0 2880 700 pts/1 R+ 06:34 0:00 grep App Is there a way of doing this? If it is, would it be reliable to use a bash script and cron for checking if the application is running? TIA Albert -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.362 / Virus Database: 267.13.3/172 - Release Date: 2005/11/15
Albert, On Wednesday 16 November 2005 23:39, Albert wrote:
Hi All,
I want to write a bash script which can be executed by cron every minute to check if an application is running. If it is not running the script should start it again.
What I normally would do to check if it is running is execute:
# ps aux | grep App
Keep in mind that such a command will return the ps output line for the grep itself, unless you use a pattern that is not self-recognizing.
root 7479 15.4 0.7 21300 7816 pts/1 Sl 06:34 0:00 /usr/local/bin/App
root 7495 0.0 0.0 2880 700 pts/1 R+ 06:34 0:00 grep App
Is there a way of doing this? If it is, would it be reliable to use a bash script and cron for checking if the application is running?
The better way to get what you want is the pidof(8) command. Check it out and you'll be able to do what you want, I think.
Albert
Randall Schulz
On Thursday 17 November 2005 02:39 am, Albert wrote:
I want to write a bash script which can be executed by cron every minute to check if an application is running. If it is not running the script should start it again.
Here's an example: # # NAME # keepup - Keep demons up and running # # SYNOPSIS # keepup # # DESCRIPTION # If any of the following processes are down, then start them up # again. This script can be run every few minutes from a crontab. daemon="fetchmail" if ! ps -aux | grep $daemon | grep -iv grep then echo Restarting $daemon date /usr/bin/fetchmail -e 30 -f /etc/fetchmailrc fi
Bruce Marshall wrote:
On Thursday 17 November 2005 02:39 am, Albert wrote:
I want to write a bash script which can be executed by cron every minute to check if an application is running. If it is not running the script should start it again.
Here's an example:
Thanks. It certainly does the job. -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.362 / Virus Database: 267.13.3/172 - Release Date: 2005/11/15
Bruce Marshall wrote:
if ! ps -aux | grep $daemon | grep -iv grep
1. why -i? grep is grep, not GrEp 2. you can do daemon=f[e]tchmail instead to avoid irrelevant hits on the grep process 3. Use checkproc for the testing instead of any of the above
On Thursday 17 November 2005 08:33 pm, Anders Johansson wrote:
Bruce Marshall wrote:
if ! ps -aux | grep $daemon | grep -iv grep
1. why -i? grep is grep, not GrEp 2. you can do daemon=f[e]tchmail instead to avoid irrelevant hits on the grep process 3. Use checkproc for the testing instead of any of the above
Picky picky picky.... <g> (But we all learn from others) I stole the script from someone else.... :-)
On Thursday 17 November 2005 08:33 pm, Anders Johansson wrote:
Bruce Marshall wrote:
if ! ps -aux | grep $daemon | grep -iv grep
1. why -i? grep is grep, not GrEp 2. you can do daemon=f[e]tchmail instead to avoid irrelevant hits on the grep process 3. Use checkproc for the testing instead of any of the above
How does one get checkproc to work? (my attempt) ======================================================= srv1:/var/mail/backup # ps aux | grep fetch | grep -v grep root 8104 0.0 0.2 5808 1832 ? Ss Nov13 0:10 fetchmail -f /etc/fetchmailrc srv1:/var/mail/backup # checkproc fetchmail srv1:/var/mail/backup # checkproc /usr/bin/fetchmail srv1:/var/mail/backup # ============================================ No response using progname or fullpath to progname
Bruce Marshall wrote:
On Thursday 17 November 2005 08:33 pm, Anders Johansson wrote:
Bruce Marshall wrote:
if ! ps -aux | grep $daemon | grep -iv grep
1. why -i? grep is grep, not GrEp 2. you can do daemon=f[e]tchmail instead to avoid irrelevant hits on the grep process 3. Use checkproc for the testing instead of any of the above
How does one get checkproc to work?
(my attempt) ======================================================= srv1:/var/mail/backup # ps aux | grep fetch | grep -v grep root 8104 0.0 0.2 5808 1832 ? Ss Nov13 0:10 fetchmail -f /etc/fetchmailrc
srv1:/var/mail/backup # checkproc fetchmail
srv1:/var/mail/backup # checkproc /usr/bin/fetchmail
Try checkproc /usr/bin/fetchmail && echo "it's running" checkproc simply returns a value, which you can then test for, for example using if, as in your previous script
Bruce, Anders, On Thursday 17 November 2005 17:52, Anders Johansson wrote:
...
How does one get checkproc to work?
...
Try
checkproc /usr/bin/fetchmail && echo "it's running"
checkproc simply returns a value, which you can then test for, for example using if, as in your previous script
Also check out the man page for options and, most of all, the two aliases and primary modes of operation: absolute path name of executable (checkproc) and base name of executable (pidofproc). Randall Schulz
On Thursday 17 November 2005 19:45, Bruce Marshall wrote:
On Thursday 17 November 2005 08:33 pm, Anders Johansson wrote:
Bruce Marshall wrote:
if ! ps -aux | grep $daemon | grep -iv grep
1. why -i? grep is grep, not GrEp 2. you can do daemon=f[e]tchmail instead to avoid irrelevant hits on the grep process 3. Use checkproc for the testing instead of any of the above
How does one get checkproc to work?
(my attempt) ======================================================= srv1:/var/mail/backup # ps aux | grep fetch | grep -v grep root 8104 0.0 0.2 5808 1832 ? Ss Nov13 0:10 fetchmail -f /etc/fetchmailrc
srv1:/var/mail/backup # checkproc fetchmail
srv1:/var/mail/backup # checkproc /usr/bin/fetchmail
srv1:/var/mail/backup # ============================================ No response using progname or fullpath to progname
If checkproc doesn't work when using the full path for the target, try just the target name. Like I said: you can modify it for your use. 8-) -- ...Yogich
* Albert
I want to write a bash script which can be executed by cron every minute to check if an application is running. If it is not running the script should start it again.
make a script as: #!/bin/bash if ! (pidof -x <app-name>); then /<path>/<to>/<app-name>& fi call the script from cron */5 * * * * /<path>/<to>/<script-name> > /dev/null -- Patrick Shanahan Registered Linux User #207535 http://wahoo.no-ip.org @ http://counter.li.org HOG # US1244711 Photo Album: http://wahoo.no-ip.org/gallery2
On Thursday 17 November 2005 01:39, Albert wrote:
Hi All,
I want to write a bash script which can be executed by cron every minute to check if an application is running. If it is not running the script should start it again.
What I normally would do to check if it is running is execute:
# ps aux | grep App
root 7479 15.4 0.7 21300 7816 pts/1 Sl 06:34 0:00 /usr/local/bin/App
root 7495 0.0 0.0 2880 700 pts/1 R+ 06:34 0:00 grep App
Is there a way of doing this? If it is, would it be reliable to use a bash script and cron for checking if the application is running?
TIA
Albert Here's one I used for checking my internet connection... perhaps you can modify it for your needs. Someone on this list was kind enough to share it with me about a year ago, or so. Cannot remember the name, unfortunately.
How to stay connectd to internet: #!/bin/bash # # Script to check if ADSL is disconnected & try reconnecting # result=$(cinternet -i dsl0 -s | grep disconnect) #Checks the status of device dsl0 for the word 'disconnect' and stores the #result in a variable called 'result' if [ -n "$result" ] #Test to see if the value is not NULL then #OK the interface is down cinternet -i dsl0 -A #Start the interface. sleep 10 else #The interface is up - Just exit exit 0 fi ____________________ EOF ______________________ HTH... -- ...Yogich
Yogich wrote:
Here's one I used for checking my internet connection... perhaps you can modify it for your needs. Someone on this list was kind enough to share it with me about a year ago, or so. Cannot remember the name, unfortunately.
Thanks. Will certainly help keeping my ADSL up (Telco drops connection every couple of hours) -- No virus found in this outgoing message. Checked by AVG Free Edition. Version: 7.1.362 / Virus Database: 267.13.3/172 - Release Date: 2005/11/15
participants (6)
-
Albert
-
Anders Johansson
-
Bruce Marshall
-
Patrick Shanahan
-
Randall R Schulz
-
Yogich