开发者

How to write a "System Process" (start up process) on the Mac using Xcode?

I need to write an app that will run in the background or as a System Process on the Mac. It needs to run at start up and needs to run even while no user is logged in.

I've got Xcode 3.2.6 (Using Xcode 4 is not an option for me here) - what t开发者_开发知识库ype of project should I use for it? When I'm ready to deploy it, how do I "install" it so that it'll auto-start when the computer boots up?

EDIT

My biggest question is what type of project to use. I need a process that will stay running and that will monitor a server and send it updated information every few minutes.

I initially created a command line tool for this. The main thread runs and immediately exists, so I have it in an infinite loop which sleeps the thread and performs any processing required by means of an NSTimer. Is this the wrong approach? Is there a better? That's what I'm after.


What you're describing is called a Daemon on Mac. You want to read the Daemons and Services Programming Guide for instructions on how to build them.

The most common form is a Launch Daemon that runs under launchd, described in the Launch Daemons and Agents docs. These are appropriate for Mac OS X 10.4 and later.

Prior to 10.4, there were "Startup Items." Do not use these unless you need to run on pre-10.4 systems.

Technical Note TN2083 (Daemons and Agents) is another document worth reading.

EDIT

Yes, the command-line template is the best starting point.

Most Daemons should be "on demand." That means they should let launchd run them when "something interesting" happens, they then should continue to run to handle additional events, and finally they should let launchd kill them when no events have come in for a while. What "interesting events" look like depends on your problem. Common events are traffic on a socket, changes to a file, or files existing in a queue directory. Launchd can handle auto-running you in any of those cases.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜