[launchd-dev] Obtaining information about a process from launchd

Quinn eskimo1 at apple.com
Thu Jul 31 02:35:05 PDT 2008


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


More information about the launchd-dev mailing list