Mac OSX下开机启动守护进程,接受远程命令,从而启动、通信cocoa app

除了上一篇转载的Mac OS X启动项详解,下面说明一下完整的后台进程方式,看下面这个表格:

Table 1-1  Types of Background Process

Type

Managed by launchd?

Run in which context?

Can present UI?

Login item

No*

User

Yes

XPC service

Yes

User

No

(Except in a very limited way using IOSurface)

Launch Daemon

Yes

System

No

Launch Agent

Yes

User

Not recommended


该表格来自:Designing Daemons and Services,其中:* Login items are started by the per-user instance of launchd, but it does not take any actions to manage them


上文提到的StartupItems方案,在Launch shell script on login in Mac OS (OS X)中提到了:


You can't just place plain scripts in that folder. You need a "specialized bundle" how Apple calls it, basically a folder with your executable, and a .plist configuration. And you should put it in/Library/StartupItems since /System/Library/StartupItems/ is reserved for the operating system. Read all about it here:

https://developer.apple.com/library/mac/documentation/macosx/conceptual/bpsystemstartup/chapters/StartupItems.html

Also note that the whole stuff is marked as deprecated technology. And that Apple is suggesting the use of launchd. There is an example how to set it up here:

http://superuser.com/questions/229773/run-command-on-startup-login-mac-os-x


在下使用了这个方案:(在Launch shell script on login in Mac OS (OS X)中第二个答案)


launchd [ALL users]

  • Place your script, e.g., Test.sh, in a SHARED location, e.g., /Users/Shared
  • Create a file with extension .plist in /Library/LaunchAgents (requires admin privileges), e.g., /Library/LaunchAgents/LoginScripts.Test.plist, by running the following in Terminal.app:
sudo touch /Library/LaunchAgents/LoginScripts.Test.plist
  • Open the file and save it with the following content (make sure your text editor prompts for admin privileges on demand; alternatively, use sudo nano /Library/LaunchAgents/LoginScripts.Test.plist):
<?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>
      <!-- YOUR SELF-CHOSEN LABEL (TASK ID) HERE -->
    <string>LoginScripts.Test.sh</string>
    <key>ProgramArguments</key>
    <array>
          <!-- YOUR LITERAL SCRIPT PATH HERE -->
        <string>/Users/Shared/Test.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

* The <!-- ... --> comments indicate the places to customize; you're free to choose a label, but it should be unique - ditto for the .plist filename; for simplicity, keep the label and the filename root the same.

  • From Terminal.app, run the following:
sudo chown root /Library/LaunchAgents/LoginScripts.Test.plist
sudo launchctl load /Library/LaunchAgents/LoginScripts.Test.plist
  • Note that, as a side effect, the script will execute right away. From that point on, the script will execute whenever ANY user logs on.
  • It is not strictly necessary to run launchctl load -- since, by virtue of the file's location, it will be picked up automatically on next login -- but it's helpful for verifying that the file loads correctly.


结合下面这个博客,就可以完成功能了:plist+sh script+command line tool+cocoa app:


服务进程+cocoa客户端解决方案:Mac OS X下从服务进程中启动另一个应用程序(Cocoa Application)


—————————————————————惭愧的分割线———————————————————————


上面写的plist+sh script+command line tool+cocoa app是逻辑上觉得可行,但是没能解决问题,


环境:MAC OSX

问题点:plist中设置shell脚本,脚本中调用可执行文件,没有任何反应(还没找到原因,希望有知道的哥们儿提点一下)

当前解决方案:去掉shell脚本,在plist中直接设置可执行文件完整路径,例子资源:mac 开机启动 案例


同样遇到类似问题的博客有:sudo在shell脚本执行的问题,有兴趣的哥们儿可以看看




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值