Go Back   The macosxhints Forums > OS X Help Requests > UNIX - General



Reply
 
Thread Tools Rating: Thread Rating: 5 votes, 5.00 average. Display Modes
Old 05-21-2004, 03:54 PM   #1
griffman
MVP
 
Join Date: Dec 2001
Location: Portland, OR
Posts: 1,472
Auto restart a crashing app?

I'm running a Evological at home for a bit; it's a webcam app. It runs fine 99% of the time, but then every so often, it barfs for some unknown reason and unexpectedly quits. Re-lauching it from the Finder works fine; no prefs or anything else needs to be dealt with.

What I'd like is a way to be able to do is restart Evological via a cronned (is that a word?) shell script. I think this is relatively straightforward, except ... OS X throws up one of those "Evological has unexpectedly quit; would you like to send an error report" dialog boxes when it dies.

So my questions are: (a) is there a prefs setting, third party app, anything, I can use to avoid/reply/remove that crash splash message? (b) what exactly would my script look like? I know what it is in theory, but I'm having trouble with reality .

The theory version of the shell script is:
Code:
ps ax | grep Evological
 if (Evological not found) then
   open /Applications/Evological.app/etc...
 endif
The 'if' bit is the part I'm having trouble with -- how do I parse the grep results? All help appreciated on both the script and annoying dialog box.

thx;
-rob.
griffman is offline   Reply With Quote
Old 05-21-2004, 06:34 PM   #2
hayne
Site Admin
 
Join Date: Jan 2002
Location: Montreal
Posts: 32,473
You could use the "-q" option to grep to make it quiet (not deliver any output) and then check the result by testing the Bourne shell variable $?
which would be 0 if there were any matches in the grep, and 1 if there were no matches. I'm assuming you are using the Bourne shell (or Bash) - you shouldn't be using the C-shell for scripts.
One awkward thing is that the invocation of the script itself will show up in the grep, so you need to use a well-known trick to sidestep that - put the first character of the name that you are searching for inside square brackets: [E]vological

Here's a sample script that shows the idea:
Code:
#!/bin/sh

# Script that checks if a specified app is running.
# You need to invoke this script with the name of the app
# but with the first letter inside square brackets
# E.g. app_running [S]afari
# (The square brackets prevent the invocation of this script
#  matching in the grep of the ps results)
# Note that case does matter: [s]afari is different from [S]afari

appName=$1
echo "Checking to see if $appName is running"

ps ax | grep -q $appName
if [ $? = 0 ]; then
    echo "$appName is running"
else
    echo "$appName is not running"
fi
[edit]
It would probably be better to use 'ps axww' to be sure that you catch the process name - some command-lines will be so long that they won't appear in full in the default 80-column width from 'ps ax'

And note that since the use of this script is slightly tricky (the need to enclose the first character of the name inside square brackets), you might want to use the method shown in the script by Rob Griffiths below, even though it is slightly less efficient (it uses an extra process for the 'grep -v')
[/edit]

And as for getting rid of the dialog box from CrashReporter, maybe this hint will help:
http://www.macosxhints.com/article.p...31206044700414
Of course that disables CrashReporter for all apps, not just Evological.

Last edited by hayne; 01-29-2005 at 07:35 PM.
hayne is offline   Reply With Quote
Old 06-02-2004, 01:58 PM   #3
griffman
MVP
 
Join Date: Dec 2001
Location: Portland, OR
Posts: 1,472
Follow-up -- I finally got around to working on this last night, and it works much as you described. I tweaked the code as I only need to test for one app, which greatly simplifies things:
Code:
#!/bin/sh
  
  ps ax | grep Evocam | grep -v grep
  if [ $? = 0 ] ; then
  	#it's up, so do nothing
  else
  	#not up, so relaunch it
  	open -a Evocam
 fi
My "grep -v grep" line is my way around grep finding itself. I tested this at work with Excel and a 1-minute cron, and it worked great -- Excel relaunched every minute!

Thanks!

-rob.
griffman is offline   Reply With Quote
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 On
HTML code is Off

Forum Jump



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


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.