<#AHLaunchCtlObjectiveCLibraryformanaginglaunchdDaemons&Agents.
使用
####注意
-
AHLaunchDomain 有五个成员,代表 LaunchDaemons 和 LaunchAgents 的常用位置。
/* User Launch Agents `~/Library/LaunchAgents`. Loaded by the console user.*/ kAHUserLaunchAgent, /* Administrator provided LaunchAgents `/Library/LaunchAgents/`. Loaded by the console user */ kAHGlobalLaunchAgent, /* Apple provided LaunchAgents `/System/Library/LaunchAgents/`. Loaded by root user.*/ kAHSystemLaunchAgent, /* Administrator provided LaunchDaemon `/Library/LaunchDaemons/`. Loaded by root user.*/ kAHGlobalLaunchDaemon, /* Apple provided LaunchDaemon `/System/Library/LaunchDaemons/`. Loaded by root user.*/ kAHSystemLaunchDaemon,
添加作业
这将加载一个作业并在适当的位置创建 launchd.plist 文件。
AHLaunchJob* job = [AHLaunchJob new];
job.Program = @"/bin/echo";
job.Label = @"com.eeaapps.echo";
job.ProgramArguments = @[@"/bin/echo", @"hello world!"];
job.StandardOutPath = @"/tmp/hello.txt";
job.RunAtLoad = YES;
job.StartCalendarInterval = [AHLaunchJobSchedule dailyRunAtHour:2 minute:00];
// All sharedController methods return BOOL values.
// `YES` for success, `NO` on failure (which will also populate an NSError).
[[AHLaunchCtl sharedController] add:job
toDomain:kAHUserLaunchAgent
error:&error];
移除作业
这将卸载作业并删除相关的 LaunchDaemons.plist 文件。
[[AHLaunchCtl sharedController] remove:@"com.eeaapps.echo"
fromDomain:kAHUserLaunchAgent
error:&error];
加载作业
简单地加载一个作业,这对于需要一次性执行的作业很好。它不会创建 LaunchDaemons 文件,但只要用户登录(对于 LaunchAgents)或系统重新启动(LaunchDaemons),就会运行指定的 LaunchDaemons 作业。
AHLaunchJob* job = [AHLaunchJob new];
// build the job as you would for adding one ...
[[AHLaunchCtl sharedController] load:job
inDomain:kAHGlobalLaunchDaemon
error:&error];
卸载作业
临时卸载作业,这将不会删除 launchd.plist 文件
[[AHLaunchCtl sharedController] unload:@"com.eeaapps.echo.helloworld"
inDomain:kAHGlobalLaunchDaemon
error:&error];
调度
要在作业中设置 StartCalendarInterval 键,请使用 AHLaunchJobSchedule 类。
+ (instancetype)scheduleWithMinute:(NSInteger)minute
hour:(NSInteger)hour
day:(NSInteger)day
weekday:(NSInteger)weekday
month:(NSInteger)month
将 AHUndefinedScheduleComponent
传递给上述任何参数将使该参数表现得像通配符。
还有一些方便的方法
+ (instancetype)dailyRunAtHour:(NSInteger)hour minute:(NSInteger)minute;
+ (instancetype)weeklyRunOnWeekday:(NSInteger)weekday hour:(NSInteger)hour;
+ (instancetype)monthlyRunOnDay:(NSInteger)day hour:(NSInteger)hour;
安装特权辅助工具
您的辅助工具必须正确签名编码,并包含嵌入的 Info.plist 和 Launchd.plist 文件。
NSError *error;
[AHLaunchCtl installHelper:kYourHelperToolReverseDomain
prompt:@"Install Helper?"
error:&error];
if(error)
NSLog(@"error: %@",error);
**有关详细信息,请查看此存储库根目录中的 HelperTool-CodeSign.py 脚本,它对获取正确的证书名称和创建 .plists 很有帮助。
请参阅 AHLaunchCtl.h 以获取完整用法。