【问题标题】:LaunchDaemon exits immediately, but keeps running when I run it directlyLaunchDaemon 立即退出,但当我直接运行它时继续运行
【发布时间】:2018-05-19 23:36:23
【问题描述】:

我以前从未编写过 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 你是我唯一的希望!

【问题讨论】:

    标签: macos daemon launchd


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-11-03
      • 2022-06-23
      • 2019-08-25
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 2015-07-13
      相关资源
      最近更新 更多