【问题标题】:Application do not start on system reboot应用程序在系统重新启动时不启动
【发布时间】:2017-08-14 21:56:41
【问题描述】:

我创建了一个运行应用程序的 LaunchDaemon 服务

sudo launchctl load /Library/LaunchDaemons/com.testapp.plist

这是我的 com.testapp.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>
    <string>com.testapp</string>
    <key>OnDemand</key>
    <false/>
    <key>UserName</key>
    <string>root</string>
    <key>GroupName</key>
    <string>wheel</string>
    <key>KeepAlive</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/var/log/OutputLog1.log</string>
    <key>StandardOutPath</key>
    <string>/var/log/OutputLog2.log</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/TestApplication/Test.app/Contents/MacOS/Test</string>
    </array>
</dict>
</plist>

它工作正常。但是当我重新启动系统时,launchdaemon 会启动我的应用程序,但它会终止应用程序。

我在控制台中得到的日志是,

20/03/17 7:15:25.239 PM Test[50]: Untrusted apps are not allowed to connect to Window Server before login.
20/03/17 7:15:25.239 PM Test[50]: Set a breakpoint at CGSLogError to catch errors as they are logged.
20/03/17 7:15:25.239 PM Test[50]: On-demand launch of the Window Server is allowed for root user only.
20/03/17 7:15:25.239 PM Test[50]: CGSLookupServerRootPort: Failed to look up the port for "com.apple.windowserver.active" (1102)
20/03/17 7:15:25.239 PM Test[50]: On-demand launch of the Window Server is allowed for root user only.
20/03/17 7:15:25.239 PM Test[50]: CGSLookupServerRootPort: Failed to look up the port for "com.apple.windowserver.active" (1102)
20/03/17 7:15:25.239 PM Test[50]: This user is not allowed access to the window system right now.
20/03/17 7:15:34.721 PM Test[50]: RegisterApplication(), FAILED TO establish the default connection to the WindowServer, CGSDefaultConnection() is NULL.
20/03/17 7:15:34.732 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.732 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.735 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.735 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.735 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.736 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.736 PM Test[50]: Invalid Connection ID 0
20/03/17 7:15:34.970 PM Test[50]: CGSLookupServerRootPort: Failed to look up the port for "com.apple.windowserver.active" (1102)
20/03/17 7:15:34.970 PM Test[50]: CGSLookupServerRootPort: Failed to look up the port for "com.apple.windowserver.active" (1102)
20/03/17 7:15:34.970 PM Test[50]: CGSLookupServerRootPort: Failed to look up the port for "com.apple.windowserver.active" (1102)
20/03/17 7:15:34.972 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.972 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.972 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.976 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.
20/03/17 7:15:34.977 PM Test[50]: CGSConnectionByID: 0 is not a valid connection ID.

我还从 Activity Monitor 检查了应用程序的运行状态,显示应用程序正在运行。

还通过

检查了守护进程状态
sudo launchctl list | grep testapp

这也表明守护进程也在运行。

另外,下面是来自守护进程 plist 的标准输出日志(它记录了我在应用程序中所做的日志):

2017-03-27 18:04:34.841 Test[802:16596] Application Started...
2017-03-27 18:04:34.896 Test[802:16596] argc : 1
2017-03-27 18:04:35.123 Test[802:16596] applicationWillFinishLaunching...
2017-03-27 18:04:35.145 Test[802:16596] CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data
2017-03-27 18:04:35.146 Test[802:16596] CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data
2017-03-27 18:04:35.147 Test[802:16596] CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data
2017-03-27 18:04:35.148 Test[802:16596] CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data
2017-03-27 18:04:35.224 Test[802:16596] applicationDidFinishLaunching...
2017-03-27 18:04:35.238 Test[802:16596] applicationDidChangeOcclusionState...
2017-03-27 18:04:35.253 Test[802:16596] applicationDidChangeOcclusionState...
2017-03-27 18:05:13.000 Test[820:17234] Application Started...
2017-03-27 18:05:13.072 Test[820:17234] argc : 1
2017-03-27 18:06:09.566 Test[92:505] Application Started...
_RegisterApplication(), FAILED TO establish the default connection to the WindowServer, _CGSDefaultConnection() is NULL.
2017-03-27 18:06:17.845 Test[92:505] argc : 1
2017-03-27 18:06:18.254 Test[92:505] applicationWillFinishLaunching...
2017-03-27 18:06:18.299 Test[92:505] CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data
2017-03-27 18:06:18.300 Test[92:505] CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data
2017-03-27 18:06:18.300 Test[92:505] CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data
2017-03-27 18:06:18.302 Test[92:505] CFPasteboardRef CFPasteboardCreate(CFAllocatorRef, CFStringRef) : failed to create global data
2017-03-27 18:06:22.918 Test[92:505] In -[NSApplication(NSQuietSafeQuit) _updateCanQuitQuietlyAndSafely], _LSSetApplicationInformationItem(NSCanQuitQuietlyAndSafely) returned error -600

我无法找到此问题的主要原因。

请帮忙。

提前致谢。

【问题讨论】:

    标签: macos launchd launch-daemon launch-agent


    【解决方案1】:

    正如this answer 所示,错误消息:

    在登录前不允许不受信任的应用程序连接到 Window Server。

    具有误导性,因为它暗示问题是信任之一,而实际上问题是您正在尝试GUI操作来自错误的上下文.

    启动守护进程独立于任何用户登录之前在系统上下文中运行,并且可能在任何用户登录之前运行,并且无法访问 Window Server - 它们无法呈现 UI。

    简而言之:如果您的应用程序呈现用户界面,它就不能作为守护进程运行。

    相比之下,启动代理(从/Library/LaunchAgents~/Library/LaunchAgents 加载)能够呈现GUI,Apple recommends against it

    也许您只需要创建一个登录项,尽管您必须基于每个用户创建它。

    有关用户登录时运行应用程序的各种方法的概述,请参阅我的this answer

    【讨论】:

    • 谢谢和抱歉,您能详细说明一下吗?另外,我正在使用来自守护进程 plist 的标准输出的日志更新我的问题。
    • @Akshada-Systematix:看看我的更新是否有帮助。你的应用实际上是做什么的?它需要以root身份运行吗?它是否需要为系统的每个用户运行?启动应用的最佳方法取决于这些问题的答案。
    • 我的应用程序的目的是在后台运行跟踪位置,它应该与引导我进入启动守护进程的任何用户无关。该应用程序将被隐藏,用户看不到。同样,我从应用程序的“MainMenu.xib”中删除了“Window”和“Main Menu”。还添加了以下行以隐藏我的应用程序。 ProcessSerialNumber psn = { 0, kCurrentProcess }; TransformProcessType(&psn, kProcessTransformToBackgroundApplication);为了隐藏我也试过了,LSUIElement请帮忙。
    • @Akshada-Systematix:改用&lt;key&gt;LSBackgroundOnly&lt;/key&gt;&lt;true/&gt;。如果这没有帮助,也许您需要创建一个 命令行工具 而不是 *.app 包。如果您有后续问题,请发布一个新问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    • 2014-02-27
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多