0

我开发了一个在 Mac 启动时运行的守护程序,这个守护程序可以正常运行,直到我安装了新的 OSX 10.8.5 [这个守护程序在 10.8.4 上正确运行]。

我不知道我的错误在哪里,

我已经创建了一个终端应用程序来编写这样的字典:

KeepAlive = 1;
Label = "vetrya.ControlloPresenza";
MachServices =     {
    "vetrya.ControlloPresenza" = 1;
};
Program = "/System/Library/VetryaControllo/ControlloPresenza";
ProgramArguments = start;
RunAtLoad = 1;
StartInterval = 20;
inetdCompatibility =     {
    Wait = 0;
};

然后在此目录中写入此字典:/System/Library/LaunchDaemons/

但是现在这个进程没有启动,有人知道这是什么原因吗?

这就是我写 dict 的方式和 plist 战争的结果正确写

NSMutableArray* arrayArguments = [[NSMutableArray alloc] initWithCapacity:1];
[arrayArguments addObject:@"/System/Library/VetryaControllo/ControlloPresenza"];
[arrayArguments addObject:@"start"];
NSDictionary* dict = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:NO],@"Wait", nil];
NSDictionary* dictNew = [[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithBool:YES],@"vetrya.ControlloPresenza", nil];

[self.controlloPresenza setObject:@"vetrya.ControlloPresenza" forKey:@"Label"];
[self.controlloPresenza setObject:@"/System/Library/VetryaControllo/ControlloPresenza" forKey:@"Program"];
[self.controlloPresenza setObject:@"start" forKey:@"ProgramArguments"]; 
[self.controlloPresenza setObject:dict forKey:@"inetdCompatibility"];
[self.controlloPresenza setObject:dictNew forKey:@"MachServices"];
[self.controlloPresenza setObject:[NSNumber numberWithBool:YES] forKey:@"KeepAlive"];
[self.controlloPresenza setObject:[NSNumber numberWithBool:YES] forKey:@"RunAtLoad"];
[self.controlloPresenza setObject:[NSNumber numberWithInt:20] forKey:@"StartInterval"];

我现在做了一些改变,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>KeepAlive</key>
    <true/>
    <key>Label</key>
    <string>vetrya.ControlloPresenza</string>
    <key>MachServices</key>
    <dict>
        <key>vetrya.ControlloPresenza</key>
        <true/>
    </dict>
    <key>Program</key>
    <string>/System/Library/VetryaControllo/ControlloPresenza</string>
    <key>ProgramArguments</key>
    <string>start</string>
    <key>RunAtLoad</key>
    <true/>
    <key>StartInterval</key>
    <integer>20</integer>
    <key>inetdCompatibility</key>
    <dict>
        <key>Wait</key>
        <false/>
    </dict>
</dict>
</plist>

我在网上发现要将守护程序注册到下一次重新启动,我可以使用这个终端命令:

sudo chown root:wheel <name.plist>
sudo chmod 644 <name.plist>

但是当我像这样创建 NSTask 时:

 NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath:_registrationPath];
    [task setArguments: @[@"sudo", @"chown",@"root:wheel",@"vetrya.ControlloPresenza.plist"]];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];
    [task launch];
    [task waitUntilExit];

    task = [[NSTask alloc] init];
    [task setLaunchPath:_registrationPath];
    [task setArguments: @[@"sudo", @"chmod",@"644",@"vetrya.ControlloPresenza.plist"]];

    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    file = [pipe fileHandleForReading];
    [task launch];
    [task waitUntilExit];

但最后我有这个错误:失败:22,“无效参数”。我怎么写这个命令?

问候

4

1 回答 1

0

可能是您刚刚从问题中显示的示例中省略了这一点,因为您说您正在写出值,但是如果您阅读有关Daemons and Services的 Apple 文档,您会看到 plist要求您指定键和值,如下所示:-

<key>KeepAlive</key>
<integer>1</integer>
<key>Label</key>
<string>"vetrya.ControlloPresenza"</string>

此外,如果这就是您正在做的事情,我看到您将程序参数设置为一个值,而不是一个字符串数组,这将使它:

<key>ProgramArguments</key>
<array>
    <string>start</string>
</array>
于 2013-09-20T12:21:19.620 回答