launchctl slow to respond?

I use launchctl to load LaunchAgents but in my script the launchctl seems very slow to respond. Even in this dummy script the ScriptEditor sometimes just hangs without signaling an error (also when it’s run as a script program it often hangs)

Anyone have any idea of what I’m doing wrong here? Could it be because I’m launching an app from a hidden folder (shouldn’t make a difference, I thought)?

set toDo to display dialog "what to do?" buttons {"load", "unload"}

set theToDo to button returned of toDo

try
	do shell script "launchctl " & theToDo & " /Library/LaunchAgents/com.tmpass.plist"
on error
	display dialog "There was an error, please try again!" buttons {"OK"} default button 1 with icon 0
end try

The LaunchAgent is also very simple:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.tmpass</string>
	<key>OnDemand</key>
	<false/>
	<key>Program</key>
	<string>/.TMpass/tmpass.app/Contents/MacOS/tmpass</string>
	<key>ProgramArguments</key>
	<array>
		<string>/.TMpass/tmpass.app/Contents/MacOS/tmpass</string>
	</array>
</dict>
</plist>

Hi,

Responding slow while loading a LaunchAgent seems to be normal.
I’ve experimented the last weeks a lot with launchd and had the same behavior.

I recommend to use also the -w flag for launchctl to set / reset the disable key

PS: I’ve posted an launchd installer/uninstaller script yesterday in Code Exchange

Hi Stefan,

Glad to know it wans’t something stupid on my part. I played with the -w flag but it generates an error in my script which I don’t understand.

I found that adding a repeat loop checking the presence of the launched process seems to stabilize my script so that it won’t hang us much…

set toDo to display dialog "what to do?" buttons {"load", "unload"}

set theToDo to button returned of toDo

try
	do shell script "launchctl " & theToDo & " /Library/LaunchAgents/com.tmpass.plist"
on error
	display dialog "There was an error, please try again!" buttons {"OK"} default button 1 with icon 0
end try

-- check the "load"/"unload'
repeat
	if theToDo is "load" then
		tell application "System Events"
			set test to (name of every process) contains "tmpass"
		end tell
		if test then exit repeat
	else if theToDo is "unload" then
		tell application "System Events"
			set test to (name of every process) contains "tmpass"
		end tell
		if not test then exit repeat
	end if
end repeat

Have you tried these commands in your plist to find out what’s happening? I haven’t used launchd so I’m not experienced to know what “normal” behavior is, but these seem to help finding problems.

– from the man page for launchd.plist
StandardOutPath
This optional key specifies what file should be used for data being sent
to stdout when using stdio(3).

 StandardErrorPath <string>
 This optional key specifies what file should be used for data being sent
 to stderr when using stdio(3).