Cocoa 的计划任务?
在 Cocoa(和 Mac OS X)中安排程序运行的最佳方式是什么:
- 当用户登录时。
- 在一天中的某些时间(例如:中午 12:00)。
- 以一定的时间间隔(例如:每两小时)。
除了调度之外,还应该能够轻松地取消调度程序,并且在用户删除应用程序时不会导致错误。
本质上,该程序是一个卫星命令行可执行文件,放置在同一 .app
包中主应用程序文件旁边。该程序的目的是在用户的主目录(~/Library/Application Support/MyApp
内)中执行一些后台数据更新。
crontab
是否适合执行此操作? crontab
的 man
页面说该功能已被 launchctl
吸收,但我似乎找不到如何安排特定时间运行该实用程序。
谢谢。
What is the best way in Cocoa (and Mac OS X) to schedule a program to be run:
- when the user logs in.
- at certain times of the day (for example: 12:00 noon).
- at certain time intervals (for example: every two hours).
Besides scheduling, it should also be easy to un-schedule the program and not cause errors should the user delete the application.
Essentially the program is a satellite command-line executable placed next to the main application's file within the same .app
bundle. The program's purpose is to do some background data updates in the user's home directory (within ~/Library/Application Support/MyApp
.)
Is crontab
a good candidate for this? The man
page for crontab
said that the functionality has been absorbed to launchctl
, but I can't seem to find how to schedule specific times to run the utility.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
launchd
是执行此操作的正确方法(在 Mac 上不推荐使用crontab
,取而代之的是launchd
)。您将创建一个 plist 文件,其中包含有关您的程序的信息(可执行文件名称、参数等),其中一部分是(在浏览器中输入的警告且未经测试):StartCalendarInterval
应该运行您的节目在第 12 个小时(也就是中午)开始。StartInterval
应每 2 小时(= 7200 秒)运行您的程序LimitLoadToSessionType
仅在 Aqua 会话开始时加载您的程序(用户实际登录到窗口服务器(因此,如果用户ssh
处于其中,这将阻止加载)RunAtLoad
告诉可执行文件在加载 plist 时运行,这与LimitLoadToSessionType< 结合使用。 /code>,应该在用户登录时启动可执行文件。
StartInterval
和StartCalendarInterval
可能是互斥的,如果是这种情况,您可以取出。 >StartInterval
键并将StartCalendarInterval
内容更改为:有关更多信息,请参阅
man launchd.plist
。launchd
is the proper way to do this (crontab
is deprecated on the Mac in favor oflaunchd
). You'd create a plist file with the information about your program (executable name, arguments, etc), and part of it would be (warning typed in a browser and untested):StartCalendarInterval
should run your program at the top of the 12th hour (so noon).StartInterval
should run your program every 2 hours (= 7200 seconds)LimitLoadToSessionType
only loads your program when an Aqua session begins (the user actually logs in to the window server (so this would prevent loading if the userssh
's in)RunAtLoad
tells the executable to run when the plist is loaded. This, combined with theLimitLoadToSessionType
, should start the executable when the user logs in.It's possible that
StartInterval
andStartCalendarInterval
are mutually exclusive. If that's the case, you can take out theStartInterval
key and change theStartCalendarInterval
stuff to:For more information, see
man launchd.plist
.有一个名为 Lingon 的漂亮 GUI 应用程序,它可以帮助编辑 launchd 配置文件。
您可以在这里下载> Lingon
There is a nice GUI app called Lingon which helps with editing launchd configuration files.
You can download it here > Lingon
在 Mac OS X 上,您可以在终端中运行“crontab -e”来启动 crontab 编辑器(默认为 Vi)。当您退出编辑器时,作业就会被安排。
On Mac OS X you can run "crontab -e" in the Terminal to start the crontab editor (Vi by default). When you quit the editor, the job is scheduled.