0

I'm using a launchd script found here to automatically re-launch an app if the app crashes:

Automatically relaunch a closed macOS application

The app has a menu bar icon. After running that script to trigger re-launching if crashed, I'm noticing a second instance of the menu bar icon appears and annoyingly flashes in the menu bar briefly every 20-30 seconds or so.

It's as if the script actually launches a new instance of the app, which then quickly terminates if an existing instance of the app is already running. When I really just want it to re-launch if closed.

Why is this second application launch occurring?

Are there any launchd / launchctl arguments that will only re-launch the program if it is no longer running?

1 Answer 1

1

I had a similar problem, but in my .plist file I was actually referencing a bash script. Within that bash script, I would start a particular program, then execute a few additional commands. The problem was that when loading the .plist file with launchctl, it would run the bash script correctly, the program would be started, however once the bash script terminated, the spawned process of the program would also be terminated. Adding the KeepAlive configuration to the .plist file did not keep the spawned program open, but instead it would just repeatedly execute the bash script (resulting in a situation similar to the "flashing" you describe).

For me, the solution was to add the following configuration in the .plist file:

<key>AbandonProcessGroup</key>
<true/>

This way, the process spawned by the bash script would be "abandoned" by launchd and thus allowed to continue running, even after the bash script exited.

If this doesn't solve your problem, then you will have to debug and see what's going on. To debug, add the following configuration:

   <key>StandardErrorPath</key>
   <string>/tmp/mycommand.err</string>
   <key>StandardOutPath</key>
   <string>/tmp/mycommand.out</string>

Then observe the two log files with tail -f .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .