为什么和如何在Cocoa / Objective-C中创建一个看门狗 [英] The Why and the How of Creating a Watchdog in Cocoa/Objective-C

查看:195
本文介绍了为什么和如何在Cocoa / Objective-C中创建一个看门狗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

周一快乐给大家!

这里是我的情况。任何帮助非常感谢!

Here is my situation. Any assistance greatly appreciated!

我创建了一个可执行文件,当安装时被放置在机器的启动代理文件夹中。在理论上,它应该永久运行,每秒收集数据。它可以在机器睡眠时停止,但在计算机重新激活时重新启动。

I have created an executable that when installed gets placed in the Launch Agents folder of the machine. In theory it should run perpetually, collecting data every second. It can stop when the machine is asleep, but restart when the computer is reactivated.

这里是我的问题:有时程序只是自发停止,通常在运行几天后。如果一个重新启动计算机,则程序重新启动,并再次运行一段时间之前,同样的问题发生。

Here is my problem: sometimes the program just spontaneously stops, usually after a couple of days of running. If one restarts the computer then the program restarts and again runs for awhile before the same problem occurs.

我不知道这个问题的原因,但朋友告诉我,我可以创建一个看门狗,每分钟都会检查我的可执行文件是否正在运行。如果不是,则看门狗将重新启动可执行文件。

I have no idea the reason for this problem, but was told by a friend that I could create a 'watchdog' that every minute would check if my executable is running. If it is not, then the watchdog will restart the executable.

有关如何执行此操作的任何指针?我的应用程序用Objective-C和Cocoa编写,并在Mac上运行。

Any pointers as to how to do this? My apps are written in Objective-C and Cocoa and run on Macs.

非常感谢!

编辑1

我会尽我所能解释应用程式的运作方式。它基本上是用C和Objective C编写的方法的集合,每秒查询系统的信息:

I will explain how the app works to the best of my ability. It is basically a collection of methods written in C and Objective C that queries information about the system every second:


  • - 传输的字节

  • 键盘活动 -

  • - 小鼠活动 -

  • - 硬盘百分比用完了

  • - 有键盘焦点的进程

  • -bytes transmitted
  • -keyboard activity-
  • -mouse activity-
  • -percentage of harddrive used up
  • -process which has keyboard focus

...等。

它使用Apple开发者网站提供的低级API。它每秒将信息写入文件。每小时压缩文件并创建一个新文件。

It does this using low level API's provided by the Apple Developer website. It writes the information to a file every second. Every hour the file is zipped up and a new file is created.

请询问是否需要更多说明:)

Please ask if more clarification is needed :)

编辑2

对于那些想知道的人,这里是plist文件

For those who are wondering, here is the plist file

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
 <plist version="1.0">
 <dict>
     <key>Label</key>
     <string>com.Intel</string>
     <key>OnDemand</key>
     <true/>
     <key>ProgramArguments</key>
     <array>
         <string>/Library/LaunchAgents/Contents/Intel</string>
     </array>
     <key>RunAtLoad</key>
     <true/>
 </dict>
 </plist>

再次感谢....

推荐答案

您可以使用launchd作为看门狗。只需向launchd plist文件中添加以下键值对:

You can use launchd as your watchdog. Simply add the following key-value pair to your launchd plist file:

 <key>KeepAlive</key>
 <true/>

这将确保您的进程如果崩溃或终止,将重新启动。 (如果你的进程挂起,这将不会有帮助。)有关KeepAlive和其他launchd键的完整文档,请在Terminal中运行man launchd.plist。

This will ensure that your process will be relaunched if it crashes or otherwise terminates. (It won't help if your process hangs, however.) For complete documentation on KeepAlive and other launchd keys, run "man launchd.plist" in Terminal.

这篇关于为什么和如何在Cocoa / Objective-C中创建一个看门狗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆