[英]How to Load LaunchDaemon plist from my Mac Application
I'm trying to a Load LaunchDaemon plist from my Mac Application. 我正在尝试从Mac应用程序加载Load LaunchDaemon plist。 When i try to load it in terminal it loads successfully, but when i try to load it thru my code, it doesn't work. 当我尝试在终端中加载它时,它成功加载,但是当我尝试通过我的代码加载时,它不起作用。
OSStatus status;
AuthorizationRef authorizationRef;
status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &authorizationRef);
if (status != errAuthorizationSuccess)
{
NSString *err = [NSString stringWithFormat:@"Error Creating Initial Authorization : %d", status];
[self ShowErrorAndExit:err];
}
//kAuthorizationRightExecute == "system.privilege.admin"
AuthorizationItem right = {kAuthorizationRightExecute, 0, NULL, 0};
AuthorizationRights rights = {1, &right};
AuthorizationFlags flags = kAuthorizationFlagDefaults |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagExtendRights;
// Call AuthorizationCopyRights to determine or extend the allowable rights.
status = AuthorizationCopyRights(authorizationRef, &rights, NULL, flags, NULL);
if (status != errAuthorizationSuccess)
{
NSString *err = [NSString stringWithFormat:@"Sorry, we need Administrator priviliges to Continue"];
[self ShowErrorAndExit:err];
}
// After getting the Authorization Reference, executing the launchctl to load my LaunchDaemon
char *daemonplist="/Library/LaunchDaemons/com.myappdaemon.plist";
char *launchctl="/bin/launchctl";
char *launchdArgs[]={"load",daemonplist,NULL};
FILE *ldpipe=NULL;
OSStatus;
status=AuthorizationExecuteWithPrivileges(authorizationRef,launchctl,kAuthorizationFlagDefaults,launchdArgs,&ldpipe);
if(status!=errAuthorizationSuccess)
{
NSLog(@"Error:%d",status);
}
if(status==errAuthorizationSuccess)
{
NSLog(@" succesfully loaded the daemon plist);
}
status=AuthorizationFree(authorizationRef,kAuthorizationFlagDestroyRights);
** **
** **
<key> Label</key>
<string>com.myappdaemon</string>
<key>Program</key>
<string>/Applications/myapplication.app/Contents/MacOS/myappdaemon</string>
<key>ProgramArguments</key>
<array>
<string>/Applications/myapplication.app/Content/MacOs/myappdaemon</string>
</array>
<key>WorkingDirectory</key>
<string>/Library/myapplication</string>
<key>StandardOutPath</key>
<string>/dev/null</string>
<key>StandardErrorPath</key>
<string>/dev/null</string>
<key>RunAtLoad</key>
</true>
<key>keepAlive</key>
<true/>
Thing is it shows the daemon plist is loaded successfully, but it's not.
And After system restart the daemon starts fine. 在系统重新启动后,守护程序将正常启动。 The plist loads fine in terminal. plist在终端中加载良好。 I have even tried with '-w' & '-F' to force load the daemon, but it doesn't loads at all. 我什至尝试使用'-w'和'-F'来强制加载守护程序,但是它根本不加载。 The weird thing is it just keeps saying that the daemon is loaded. 奇怪的是,它一直说守护程序已加载。 Now what am i doing wrong here ..? 现在我在这儿做错了..?
AuthorizationExecuteWithPrivileges
is Deprecated in OS X v10.7. OS X v10.7中不推荐使用 AuthorizationExecuteWithPrivileges
。 You can use applescript
您可以使用applescript
do shell script "launchctl load /Library/LaunchDaemons/com.yourcompany.app.plist" with administrator privileges
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.