LaunchDaemon会立即退出,但在我直接运行时会继续运行

时间:2017-12-06 06:41:32

标签: macos daemon launchd

我之前从未写过LaunchDaemon,所以我从一个简单的开始。即便是我的简单也不会执行。

我正在运行macOS Sierra 10.12.5。

编辑:我的system.log充满了以下内容:

Dec  6 00:02:47 Michaels-Mac-mini com.apple.xpc.launchd[1] (com.frescologic.hello): Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
Dec  6 00:02:57 Michaels-Mac-mini com.apple.xpc.launchd[1] (com.frescologic.hello[1386]): Service could not initialize: 16F73: xpcproxy + 11769 [1505][34964CF1-9965-3B4D-ADC7-6FBC6669C56D]: 0x2

但是,当我直接从命令行运行hello时,它会继续运行。

我的配置文件:

<?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.frescologic.hello</string>
    <key>ProgramArguments</key>
    <array>
        <string>hello</string>
        <string>world</string>
    </array>
    <key>KeepAlive</key>
    <true/>
</dict>

我的守护进程的来源:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int
main(int argc, const char * argv[])
{
    FILE *outFile;

    if ( argc != 2 ){

        fprintf( stderr, "Usage:\n $ %s world\n", argv[ 0 ] );
        exit( 1 );
    }

    outFile = fopen( "/tmp/foo", "w" );
    if ( outFile == NULL ) exit( 1 );

    while ( 1 ){
        fprintf( outFile, "%s\n", argv[ 1 ] );
        sleep( 10 );
    }

    return 0;
}

相关权限:

$ ls -l /Library/LaunchDaemons/com.frescologic.hello.plist 
-rw-r--r--@ 1 root  wheel  375 Dec  5 21:47 /Library/LaunchDaemons/com.frescologic.hello.plist

$ ls -l /usr/local/libexec
total 40
-rwxr-xr-x  1 root  wheel  18240 Dec  5 21:45 hello

Launchctl声称它正在运行:

$ sudo launchctl list | grep hello
Password:
-   78  com.frescologic.hello

但ps没有:

$ ps -ef | grep hello
  501   737   387   0 10:38PM ttys000    0:00.00 grep hello

$ ps -ax | grep hello
  743 ttys000    0:00.00 grep hello

日志文件不存在:

$ cd /tmp
$ ls
com.apple.launchd.JOJDWGHX78    com.apple.launchd.oUj51Uvj6v

帮助我O-Stackoverflow你是我唯一的希望!

1 个答案:

答案 0 :(得分:0)

Stephàne在darwin-drivers@lists.apple.com上回答了这个问题。使用可执行文件的完整路径将“Program”键添加到属性列表中:

<key>Program</key>
<string>/usr/local/libexec/hello</string>