0

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">
<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!

4
  • what does ps -ef | grep Intel | grep -v grep | wc -l or ps -ef | grep Intel | grep -v grep | wc -l output? the problem could be that the process somehow is in still there. Commented Sep 6, 2010 at 19:20
  • Checks to see if the process Intel is running :) Commented Sep 6, 2010 at 19:21
  • sorry I accidentally sent, check now :) Commented Sep 6, 2010 at 19:21
  • Well... a quick solution would then be to kill that process at the end of the script, no? If that makes sense, how would I do that? Commented Sep 6, 2010 at 19:41

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Gordon, thanks for all the effort. Please check my edit and let me know what you think. Thanks again ;)
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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.