守护进程与可可
我写了一个 FTP 服务器,它是通过 ftpd.command 从命令行启动的。 现在我想从 Cocoa 应用程序运行该命令。
我想退出应用程序并且命令应保持运行。 当我返回 Cocoa 应用程序时,它应该知道 FTP 服务器是否仍在运行。
有人可以帮助我吗? 多谢! 朱利安
I wrote a FTP Server, witch is started from the command line via ftpd.command.
Now I want to run that command from a Cocoa app.
I want to quit the app and the command should remain running.
And when I return to the Cocoa app, it should know, if the FTP Server is still running.
Is anybody out there who can help me?
Thanks a lot!
Julian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
continue
You should be using
launchd
to handle the FTP server. It is specifically designed for the situation you describe, namely launching and managing background services.You need to create a
launchd
configuration file, which can be placed in one of these locations:~/Library/LaunchAgents
: Per-user agents provided by the user./Library/LaunchAgents
: Per-user agents provided by the administrator./Library/LaunchDaemons
: System-wide daemons provided by theadministrator.
A daemon is a system-wide service of which there is one instance for all clients. An agent is a service that runs on a per-user basis.
launchd
Configuration files are in the form of a property list.You need to create a
launchd
configuration plist and place it in one of the above locations. You can configure the plist so thatlaunchd
runs your service at launch or periodically, or in response to various actions (such as the contents of a folder changing).To check to see whether or not your job is running, you need to use the Service Management framework. You can ask
launchd
for the status of your job like so:If the job could not be found then
jobDict
will beNULL
.(If you're using a system-level daemon then you would replace
kSMDomainUserLaunchd
withkSMDomainSystemLaunchd
).我认为您必须调整您的 FTP 服务器,以便在完成之前它不会“阻止”命令行。我在这里找到了一些示例代码:
I think you will have to adjust your FTP server so that it won't "block" the command line until it's finished. I have found some sample code here:
查看 GitHub 上我的项目 TabletMagic 的源代码。它有一个可启动用户空间守护进程的 Cocoa 首选项窗格,此外它还管理一个 launchd 项目。 Cocoa 部分使用外部帮助程序来完成其所有特权任务,这是在第一次启动时自行设置的 - 由 Cocoa 应用程序在特权上下文中请求管理员授权。尽管不是纯可可,但这都是标准程序。
Take a look at the source code for my project TabletMagic on GitHub. It has a Cocoa preference pane which launches a user space daemon, plus it manages a launchd item. The Cocoa portion uses an external helper to do all its privileged tasks, which is self-setuid the first time it's launched - in a privileged context by the Cocoa app asking for admin authorization. This is all standard procedure, despite not being pure Cocoa.