码迷,mamicode.com
首页 > 其他好文 > 详细

如何在Mac OS X上创建一个Service服务进程

时间:2014-08-25 15:14:34      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   使用   io   strong   文件   

在Mac上,共有4钟后台进程类型,可以参考Designing Daemons and Services。要实现启动服务,又不依赖于用户,可以使用Launch Daemon。

创建Launch Daemon

  • 运行Xcode,创建一个简单的命令行应用WebTwainService

    #import <Foundation/Foundation.h>
     
    int main(int argc, const char * argv[])
    {
     
        @autoreleasepool {
     
            // insert code here...
            NSLog(@"Hello, World!");
     
        }
     
        while (1)
        {
            sleep(100);
        }
        return 0;
    }
  • 编译工程,把WebTwainService放到/Applications/Dynamsoft/WebTwainService.

  • 参考 Create Launch Daemons and Agents,创建配置文件com.dynamsoft.WebTwainService.plist.

bubuko.com,布布扣

    • StandardErrorPath & StandardOutPath: service日志文件路径

    • KeepAlive. 设置成true. 一旦WebTwainService异常崩溃, 系统会自动重启服务。

    • Label: 一个唯一值

    • ProgramArguments: 可执行文件路径

  • 这个plist配置文件可放置的地方包括:

    • ~/Library/LaunchAgents

    • /Library/LaunchAgents

    • /Library/LaunchDaemons

    • /System/Library/LaunchAgents

    • /System/Library/LaunchDaemons

  • com.dynamsoft.WebTwainService.plist放到/Library/LaunchDaemons

  • 要成功注册服务,还必须修改目录权限:sudo chown root  /Library/LaunchDaemons/com.dynamsoft.WebTwainService.plist.

  • 命令行中输入sudo launchctl运行launchd

  • 注册服务load -D system /Library/LaunchDaemons/com.dynamsoft.WebTwainService.plist。然后我们可以看到服务被加载了:

    bubuko.com,布布扣

  • 输入list可以查看运行的进程

    bubuko.com,布布扣

  • 现在重启系统,然后打开日志/var/log/webtwain.log,查看服务是否在运行了:

bubuko.com,布布扣

如何在Mac OS X上创建一个Service服务进程

标签:des   style   http   color   os   使用   io   strong   文件   

原文地址:http://my.oschina.net/yushulx/blog/306339

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!