3

I am trying to add an application as login item for all users by creating an launchd plist and copying it in /Library/LaunchAgents and also by loading it.

Plist is

<?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.testapp.UserAgent</string>
        <key>ProgramArguments</key>
        <array>
                <string>/Applications/TestAgent.app/Contents/MacOS/TestAgent</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>KeepAlive</key>
        <false/>
</dict>
</plist>

RunAtLoad will launch the application at every login. It is working for me when I login in to all different user accounts in my machine but if I quit it manually then also it is launching by itself.

How can I make it to launch only once for each login and if I quit it then it should not be launched by itself.

MacDeveloper
  • 1,334
  • 3
  • 16
  • 49

1 Answers1

2

The best resource to know how to use launchd, the daemon that launches processes on macOS is here:

https://www.launchd.info

In here, it is well explained what other settings you can use apart from RunAtLoad or together with it.

For example, in your case, you could use the key KeepAlive as follows:

 <key>KeepAlive</key>
 <dict>
    <key>SuccessfulExit</key>
    <false/>
 </dict>

This would only relaunch your process if it exited with a code different than 0, which normally denotes some abnormal situation or error. If the exit is normal then your process will not be relaunched by launchd.

Check the tab Configuration on this site to know which other configurations you could use.

jvarela
  • 3,744
  • 1
  • 22
  • 43
  • I tried this but still my application is being launched after certain time.More precisely when I login in to my system my launch agent starts the service and application is being launched. After some time I am manually quitting the application and launching it manually later after some time again another instance of application is getting launched. When I check launchctl I see one service with application bundle identifier having pid and this launch agent service with pid – MacDeveloper Jul 27 '19 at 18:29
  • Please refer my question to see my plist file – MacDeveloper Jul 28 '19 at 07:29
  • Please remove the RunAtLoad setting. KeepAlive should be enough. – jvarela Jul 28 '19 at 12:41
  • If RunAtLoad is false then my application will not be launched when user logins – MacDeveloper Jul 28 '19 at 16:14
  • No, just remove the key RunAtLoad and its value altogether from your plist. Do not set it to false. – jvarela Jul 28 '19 at 23:12
  • No I am setting it to true RunAtLoad – MacDeveloper Jul 29 '19 at 05:56