Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I created a pkg package to create a LaunchAgent to start a daemon, and in the post-install script file used this way to start the daemon:

launchctl load -s /Library/LaunchAgent/file-name.plist

It worked well.

However there is a problem: the first time the daemon started, while killing this daemon and re-installing LauchAgent, it could not start or kill the daemon. It will say this in the terminal:

file-name.plist: Already loaded

If I reboot or logout, it can start correctly. How can I fix this?


Modifying my y post-install script file to:

sudo -u $USER /bin/launchctl unload -S Aqua /Library/LauchAgent/file-name.plist
sudo -u $USER /bin/launchctl load -S Aqua /Library/LauchAgent/file-name.plist

as per @Daniel’s comment did fix the previous problem. That worked well on Mac OS X 10.6 and 10.7, but on 10.5, the daemon could not start this way. I tried the script file in the terminal after installation. Here is some information:

launchctl:Error unloading: file-name.plist
launch_msg():Socket is not connected
share|improve this question
    
launchctl unload? –  Daniel Beck Oct 25 '11 at 6:26
1  
Please consider editing the title of your question to better mirror its current focus, i.e. the “launch_msg():Socket is not connected” issue. –  kopischke Oct 25 '11 at 10:04

2 Answers 2

up vote 2 down vote accepted

The issue you have encountered crops up when you try to load a LaunchAgent while more than one user is logged in (i.e. via Fast User Switching). See this message by Quinn “The Eskimo” from Apple Developer Relations on the launchd mailing list, and, for more details, this message of his on Old Nabble. Refer there for solutions (TL;DR: reboot).

Note I’m not entirely sure if the problem only exists in 10.5 – I couldn’t locate the Radar issue 5476420 linked to by Quinn on Open Radar to confirm its status.

share|improve this answer
    
while I removed "sudo -u $USER", the daemons started as root, but i want it run as user.. Is there only way to reboot to fix it? –  viwenver Oct 26 '11 at 1:24
    
As explained in detail by Quinn, LaunchAgents need to run in the current user’s Mach context. On leopard at least, they cannot attach to those contexts for any user but the one installing the agent – which means that, with several users logged in, you need to reboot to update the Agent for all of them. –  kopischke Oct 26 '11 at 8:45

For it to work in 10.5, use su instead of sudo:

su "$USER" -c "/bin/launchctl unload -S Aqua /Library/LauchAgent/file-name.plist"
su "$USER" -c "/bin/launchctl load -S Aqua /Library/LauchAgent/file-name.plist"

I don’t know why they behave differently.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.