2017-03-22 171 views
1

我創建了運行的應用程序應用程序不啓動在系統重新啓動

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

這裏是我的com.testapp.plist一個LaunchDaemon服務:

<?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. 

我還檢查應用程序從活動監控運行狀態,這表明應用程序正在運行。

通過

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 

我不能夠得到這個問題的主要原因。

請幫忙。

在此先感謝。

回答

1

由於this answer顯示,錯誤消息:

不受信任的應用程序是不允許登錄之前連接到窗口服務器。

是誤導的,因爲它表明這個問題是信任一個,當現實中的問題是,你正在試圖GUI操作從錯誤的上下文

啓動守護進程獨立於系統上下文運行,並且可能在任何用戶登錄之前運行,並且無法訪問Window Server - 它們無法呈現UI。

總之:如果您的應用程序提供了一個用戶界面,它不能運行爲守護程序

相比之下,發射(從/Library/LaunchAgents~/Library/LaunchAgents加載)能夠呈現的GUI,Apple recommends against it的。

也許你需要的僅僅是創建一個登錄項,儘管你必須在每個用戶的基礎上創建它。

有關每當用戶登錄時運行應用程序的各種方式的概述,請參閱我的this answer

+0

感謝和抱歉,但請你詳細說明一樣嗎?另外,我正在使用守護進程plist的stdout日誌更新我的問題。 –

+0

@ Akshada-Systematix:看看我的更新是否有幫助。你的應用程序實際上做了什麼?它需要以root身份運行嗎?是否需要爲系統的每個用戶運行?啓動應用程序的最佳方法取決於這些問題的答案。 – mklement0

+0

我的應用程序的目的是在後臺運行軌道位置,它應該是不管任何用戶將我引向launchdaemon。該應用將被隱藏,並且不會被用戶看到。同樣,我從應用程序的「MainMenu.xib」中刪除了「窗口」和「主菜單」。也添加下面的行來隱藏我的應用程序。 ProcessSerialNumber psn = {0,kCurrentProcess}; TransformProcessType(&psn,kProcessTransformToBackgroundApplication); 要隱藏我也試過, LSUIElement 請幫忙。 –

相關問題