-
Notifications
You must be signed in to change notification settings - Fork 7
/
tasks.go
53 lines (44 loc) · 1.13 KB
/
tasks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package alive
import (
"fmt"
"time"
"variant/log"
"github.com/capnspacehook/taskmaster"
)
func SetWinTask(execPath string, taskPath string) {
// 创建初始化计划任务
taskService, _ := taskmaster.Connect()
defer taskService.Disconnect()
// 定义新的计划任务
newTaskDef := taskService.NewTaskDefinition()
// 添加执行程序的路径
newTaskDef.AddAction(taskmaster.ExecAction{
Path: "cmd.exe /c start /b",
Args: execPath,
})
// 定义计划任务开始执行的时间
startTime := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 8, 0, 0, 0, time.Local)
// 基于时间的触发器
newTaskDef.AddTrigger(
taskmaster.DailyTrigger{
TaskTrigger: taskmaster.TaskTrigger{
Enabled: true,
StartBoundary: startTime,
},
DayInterval: 1,
})
// 基于事件的触发器
newTaskDef.AddTrigger(
taskmaster.LogonTrigger{
TaskTrigger: taskmaster.TaskTrigger{
Enabled: true,
StartBoundary: startTime,
},
},
)
// 创建计划任务
_, _, err := taskService.CreateTask(fmt.Sprintf("\\%s", taskPath), newTaskDef, true)
if err != nil {
log.Fatal(err)
}
}