Obtaining information about a process from launchd
Hi all, Sorry for the somewhat basic question, I am writing a server in python that I have registered with launchd. I am using a preference pane to control the service (using "launchctl start/stop"), but I would like the preference pane to know if the service is actually running. Because it is a python script, the process shows up just as 'Python', so tracking the process using NSWorkspace launchedApplications (actually I just read this may not work anyway) would not work if the user happens to be using other python scripts. Is there a way to get the status of my process from launchd? Since launchd is controlling the whole show, I thought there must be some method to find out if my python script is actually running, stopped, dead, whatever. Thankyou! Chris.
At 14:09 +1000 31/7/08, Chris Stevens wrote:
Is there a way to get the status of my process from launchd? Since launchd is controlling the whole show, I thought there must be some method to find out if my python script is actually running, stopped, dead, whatever.
You can do this using <x-man-page://1/launchctl>'s "list" command. For example: $ sudo launchctl list com.anarchistturtle.FluffyProxy-ssh { "Label" = "com.anarchistturtle.FluffyProxy-ssh"; "LimitLoadToSessionType" = "System"; "OnDemand" = false; "LastExitStatus" = 0; "PID" = 14348; "TimeOut" = 30; "StandardOutPath" = "/var/log/FluffyProxy.log"; "StandardErrorPath" = "/var/log/FluffyProxy.log"; "ProgramArguments" = ( "/MyLaunchDaemons/FluffyProxy-ssh.sh"; ); }; shows that the process associated with the job if it's actually running (it has a "PID" property). I believe that you can this info programmatically using the launchmsg API with the LAUNCH_KEY_GETJOB command. The tricky part is updating the status dynamically without polling. For the running-to-not-running case, that's easy. Just use a kqueue with NOTE_EXIT. For the not-running-to-running case, I can't think of any good solutions. You might just want to have your daemon post a notification (using <x-man-page://3/notify>) when it starts up. <http://developer.apple.com/samplecode/NotifyTool/index.html> S+E -- Quinn "The Eskimo!" <http://www.apple.com/developer/> Apple Developer Relations, Developer Technical Support, Core OS/Hardware
participants (2)
-
Chris Stevens
-
Quinn