首先了解一下Mac OS X中几种守护进程:
~/Library/LaunchAgents Per-user agents provided by the user. /Library/LaunchAgents Per-user agents provided by the administrator. /Library/LaunchDaemons System-wide daemons provided by the administrator. /System/Library/LaunchAgents Per-user agents provided by Mac OS X. /System/Library/LaunchDaemons System-wide daemons provided by Mac OS X.1:添加用户的启动项
Mac OS X中,用户开机项位于~/Library/LaunchAgent, 因为~/Library是隐藏文件,所以可以打开终端(terminal),输入open ~/Library/LaunchAgents/就可以找到该目录,其中的plist文件就是启动项啦(将在下一博客中详细介绍plist文件中的内容)。
通过该启动项加载的程序或者进程在活动监视器中是退出不了的,退出了就会再次启动,解决此问题的方法
方法一:直接终端命令:
unload 后面加上plist文件的全路径(可以直接拖拽文件到终端,路径自己就添加了,很方面)
方法二:使用程序执行AppleScript:
NSString *dst = [NSHomeDirectory() stringByAppendingPathComponent:@"/Library/LaunchAgents + plist文件全名"];
NSAppleScript *cmd = [[[NSAppleScriptalloc]initWithSource:[NSStringstringWithFormat:@"do shell script \"launchctl unload %@\"",dst]]autorelease];
NSDictionary *dic = nil;
[cmd executeAndReturnError:&dic];
if(!dic){
//表示执行成功啦.
}
会了上面的方法,那么要加载一个启动项也就是很轻松的啦,只需要将unload 换成load就好了,例如
load后面加上plist文件的全路径就好了。
2:root的进程:该进程对当前电脑的所有用户都有效,权限最高,需要输入root的密码。同样打开plist的文件夹看看,
执行删除的方法同样有2种:
方法一:终端命令
后面加上plist路径,然后,会让你输入root密码(密码不会显示出来),这样就搞定了。
方法二:还是通过代码执行脚本
NSString* command =[NSStringstringWithFormat:@"do shell script \"launchctl unload %@" with administrator privileges",path];然后系统会自动要求用户输入密码。这样就搞定。
如果要执行,同上面执行一样,将unload 改为load就好了。