MacOS学习(六) 给Mac添加定时任务

Mac OS X 的 Launch Daemon / Agent

苹果的官方文档_有能力最好还是读这个,译文难免有描述不准确的地方

今天折腾了一个小需求,我们先手动实现一下添加任务的操作!

谈谈Launch

在Mac OS X 10.4以后,苹果开始使用launchd来管理所有的Process、Application 及 Script。Launch管理的这些进程分为四种:

  1. Launch Daemon:在开机时加载
  2. Launch Agent:在用户登录时加载
  3. XPC Service:
  4. Login Items:

下面两种暂时还不会用 = =||,所以先说说前面两种的简单使用。

Launch Daemon 和 Launch Agents

这两个东西其实是相同的,不同的只是他们的加载时机。Launchd是通过.plist来得知系统中有哪些东西需要被管理的。所以简单的来说,想要新增被管理项,本质上就是新增一个.plist放入苹果的管理文件夹下,然后使其被加载后执行。苹果根据用户的角色提供了不同的Launch存放位置:

~/Library/LaunchAgents           # 当前用户定义的任务
 /Library/LaunchAgents           # 系统管理员定义的任务
 /Library/LaunchDaemons          # 管理员定义的系统守护进程任务
 /System/Library/LaunchAgents    # 苹果定义的任务
 /System/Library/LaunchDaemons   # 苹果定义的系统守护进程任务

很显然,我们是最好不要使用下面两个位置的,而管理员权限比较大,这里我用到的是第一个位置。只为当前用户定任务。进入该目录,创建一个com.hello.plist。





    
    Label    
    com.hello
    
    RunAtLoad
    
    
    ProgramArguments
         
    
      /Users/chengliqing/Desktop/temp/test.sh
    
  
    
    StartInterval
    60
  
    
    StartCalendarInterval
    
        
            Weekday  
            1
            Hour     
            8
            Minute   
            58
        
        
            Weekday
            2
            Hour
            8
            Minute
            52
        
    
    
    StandardOutPath
    /Users/chengliqing/Desktop/temp/stdout.log
    
    StandardErrorPath
    /Users/chengliqing/Desktop/temp/stderr.log


StartCalendarInterval的例子

StartCalendarInterval

  Minute
  0


StartCalendarInterval

  Hour
  3
  Minute
  55


StartCalendarInterval

  Hour
  3
  Minute
  15
  Weekday
  6

写完后可以用plutil -lint xxx.plist验证一下,随意~

Launchctl的基本使用

我们将我们的任务描述出来了,接下来就该使用了!

# 加载任务
launchctl load ~/Library/LaunchAgents/com.hello.plist
# 强制加载任务, -w选项会将plist文件中无效的key覆盖掉
launchctl load -w ~/Library/LaunchAgents/com.hello.plist

# (-w强制)移除任务
launchctl unload ~/Library/LaunchAgents/com.hello.plist
launchctl unload -w ~/Library/LaunchAgents/com.hello.plist

# 手动执行任务
launchctl start com.hello

# 列出所有任务
launchctl list

# 查看任务列表, 使用 grep '任务部分名字' 过滤
$ launchctl list | grep 'com.hello'

在使用launchctl list的时候回列出所有任务能够看到任务的状态(status),如果出现非0的状态码就表示任务出错了,可以使用:launchctl error [errorCode]来查看。

test.sh

#!/bin/sh
say lalala

这个脚本的意思是让Mac说lalala

写完并保存后记得将其变为可执行的sh文件,使用

chmod a+x /Users/chengliqing/Desktop/temp/test.sh

到这里,你应该已经会基本的手动添加任务的操作了。接下来我们在代码中添加任务。

这里涉及到权限问题,cocoa application 访问了除沙盒之外的文件且想要在上架到AppStore,是需要授权的,这里先将沙盒关闭,就可以直接写文件到我们指定的路径了。注意这样是不能上架到AppStore的!

这里涉及到使用shell脚本的情况,所以先贴出执行shell脚本的代码。

func runCommand(launchPath: String, arguments: [String]) -> String {
    let pipe = Pipe()
    let file = pipe.fileHandleForReading
    
    let task = Process()
    task.launchPath = launchPath
    task.arguments = arguments
    task.standardOutput = pipe
    task.launch()
    
    let data = file.readDataToEndOfFile()
    return String(data: data, encoding: String.Encoding.utf8)!
}

先说说步骤:

  1. 先将*.plist复制到指定路径
  2. 注册任务

然后准备好我们的plist文件,这里我们将文件写到~/Library/LaunchAgents/,直接使用复制的形式,先把plist拖入项目,然后复制到指定路径。

// 文件拷贝如指定路劲
let fromPath = Bundle.main.path(forResource: "task01", ofType: "plist")
let toPath = "/Users/chengliqing/Library/LaunchAgents/com.hello.plist"
try! FileManager.default.copyItem(atPath: fromPath!, toPath: toPath)
// 执行shell  注册
let result = runCommand(launchPath: "/Users/chengliqing/Desktop/temp/loadTask.sh", arguments: ["SPHardwareDataType"])
print(result)

接下来贴出loadTask.sh的内容

# 进入到根路径(这里之所以要进入到根,是因为我们项目到时候启动的路径会发生变化,所以为确保最终查找路径的正确性所以从根路径开始查找)
cd /                                        
cd Users/chengliqing/Library/LaunchAgents/  # 进入到某用户的任务路径
launchctl load clq.hello.plist              # 注册

到这里,在不上架到AppStore的情况下,我们的App就可以随意创建任务了。

以上的方法是不能上架到AppStore的

以上的方法是不能上架到AppStore的

以上的方法是不能上架到AppStore的

你可能感兴趣的:(MacOS学习(六) 给Mac添加定时任务)