简体   繁体   English

Mac OS X的命令行应用程序

[英]Command line application for Mac OS X

I have an existing "command line tool" project. 我有一个现有的“命令行工具”项目。 This tool runs forever until user close terminal. 该工具将永远运行直到用户关闭终端为止。 There is not any method which will be called when application will terminate. 当应用程序终止时,不会调用任何方法。 How can I set a applicationWillTerminate like delegate method in this project. 我如何在这个项目中设置applicationWillTerminate像委托方法。 The need is to receive notification when user click close on terminal window. 当用户单击终端窗口上的关闭时,需要接收通知。

Also is there any way to get notification when user exit the command by pressing CTRL+C? 当用户通过按CTRL + C退出命令时,还有什么方法可以获取通知?

You should be able to trap the signals from Unix. 您应该能够捕获来自Unix的信号。

signal(SIGABRT, SignalHandler);
signal(SIGILL, SignalHandler);
signal(SIGSEGV, SignalHandler);
signal(SIGFPE, SignalHandler);
signal(SIGBUS, SignalHandler);
signal(SIGPIPE, SignalHandler);

Then define a signal handler: void SignalHandler(int signum) { ... } 然后定义一个信号处理程序: void SignalHandler(int signum) { ... }

Here's an example . 这是一个例子

Also you can read on signal handling , it should help. 您也可以阅读信号处理 ,它会有所帮助。 This might also be useful even though it's for iOS. 即使它适用于iOS, 也可能有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM