tags:

views:

47

answers:

1

Hello generous computerists!

So I have a launchd plist item that is calling the below shell script every thirty seconds. It checks to see if a program is running and if it isn't, it restarts it. Or at least that is what it's supposed to do.

The problem is, even when the process has been killed, the shell script is stating that the process is still running. I think this is because somehow the boolean is not being reset (Or something along those lines). Any help?

 n=`ps -ef | grep Intel | grep -v grep | wc -l`

 if [ $n -gt 0 ]

 then

       echo `date` CURRENTLY RUNNING. >> /Library/A_Intel_WATCHDOG/A_Intel_WatchLog.txt

 else

  echo `date` not running. ATTEMPTING RESTART... >> /Library/A_Intel_WATCHDOG/A_Intel_WatchLog.txt
  cd /Library/LaunchAgents/
  launchctl load com.Intel.plist

 fi

EDIT 1

It has been suggested that adding a 'Keep Alive' argument may be a good solution to my general problem. Any comments?

Here would be my updated plist file which should guarantee that my app keeps running perpetually:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"&gt;
<plist version="1.0">
<dict>
        <key>Label</key>
    <string>com.Intel</string>
    <key>OnDemand</key>
    <true/>
    <key>ProgramArguments</key>
    <array>
         <string>/Library/LaunchAgents/Contents/Intel</string>
    </array>
     <key>RunAtLoad</key>
    <true/>
     <key>KeepAlive</key>
    <true/>
</dict>
</plist>

I would love to hear if people think this is correct or not. Thanks so much!

+1  A: 

To troubleshoot the immediate problem, try logging the output of the ps command:

processes=`ps -ef | grep [I]ntel`
if [ -n "$processes" ]
then
    echo `date` CURRENTLY RUNNING. "$processes" >> /Library/A_Intel_WATCHDOG/A_Intel_WatchLog.txt
...

I suspect there's something else running (maybe even the script itself) matching Intel; this'll tell you in the log.

However, I think this is irrelevant, because this script seems to be trying to solve a problem that's better solved elsewhere: launchd is entirely capable of monitoring and restarting the processes it manages (it's one of its significant features). Just add

<key>KeepAlive</key>
<true/>

to /Library/LaunchAgents/com.Intel.plist, and launchd will restart the program itself.

BTW, if you for some reason did need to manually restart a launchd-managed process, launchctl load is the wrong incantation -- you want launchd start.

Gordon Davisson
Hi Gordon, thanks for all the effort. Please check my edit and let me know what you think. Thanks again ;)
Eric Brotto
I'd remove the `<key>OnDemand</key> <true\>` bit (it's a deprecated way of saying *not* to jeep the job alive, and I'm not sure how it'll interact with KeepAlive), and move the script/program to somewhere other than /Library/LaunchAgents/ (the only things that should be in there are .plist files; maybe create /Library/Scripts/ and put it in there).
Gordon Davisson
@Gordon, Thanks so much!
Eric Brotto