---
title: "定时任务-基本使用"
url: "https://goframe.org/docs/components/os-gcron-example"
markdown_url: "https://goframe.org/docs/components/os-gcron-example.md"
locale: "zh-Hans"
version: "2.10.x(Latest)"
section: "组件列表"
category: "组件列表 / 系统相关 / 定时任务-gcron"
published: "2024-10-21"
updated: "2024-11-13"
description: "在GoFrame框架中使用gcron来管理定时任务。你将学习如何添加、启动、停止、删除和搜索定时任务。此外，还涵盖了单例定时任务、单次定时任务以及运行指定次数的任务等高级功能。这些功能帮助开发者更高效地管理和调试应用内的定时任务，提高应用的性能和可靠性。"
keywords: ["定时任务", "GoFrame", "GoFrame框架", "gcron", "单例定时任务", "单次定时任务", "指定次数任务", "任务搜索", "任务停止", "任务删除"]
---

# 定时任务-基本使用

## 基本使用

```go
package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main() {
    var (
        err error
        ctx = gctx.New()
    )
    _, err = gcron.Add(ctx, "* * * * * *", func(ctx context.Context) {
        g.Log().Print(ctx, "Every second")
    }, "MySecondCronJob")
    if err != nil {
        panic(err)
    }

    _, err = gcron.Add(ctx, "0 30 * * * *", func(ctx context.Context) {
        g.Log().Print(ctx, "Every hour on the half hour")
    })
    if err != nil {
        panic(err)
    }

    _, err = gcron.Add(ctx, "@hourly", func(ctx context.Context) {
        g.Log().Print(ctx, "Every hour")
    })
    if err != nil {
        panic(err)
    }

    _, err = gcron.Add(ctx, "@every 1h30m", func(ctx context.Context) {
        g.Log().Print(ctx, "Every hour thirty")
    })
    if err != nil {
        panic(err)
    }

    g.Dump(gcron.Entries())

    time.Sleep(3 * time.Second)

    g.Log().Print(ctx, `stop cronjob "MySecondCronJob"`)
    gcron.Stop("MySecondCronJob")

    time.Sleep(3 * time.Second)

    g.Log().Print(ctx, `start cronjob "MySecondCronJob"`)
    gcron.Start("MySecondCronJob")

    time.Sleep(3 * time.Second)
}
```

执行后，输出结果为：

```text
[
    {
        Name: "MySecondCronJob",
        Job:  0x14077e0,
        Time: "2021-11-14 12:13:53.445132 +0800 CST m=+0.006167069",
    },
    {
        Name: "cron-1",
        Job:  0x14078a0,
        Time: "2021-11-14 12:13:53.44515 +0800 CST m=+0.006185688",
    },
    {
        Name: "cron-2",
        Job:  0x1407960,
        Time: "2021-11-14 12:13:53.445161 +0800 CST m=+0.006196483",
    },
    {
        Name: "cron-3",
        Job:  0x1407a20,
        Time: "2021-11-14 12:13:53.445218 +0800 CST m=+0.006252937",
    },
]
2021-11-14 12:13:54.442 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:55.441 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:56.440 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:13:56.445 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} stop cronjob "MySecondCronJob"
2021-11-14 12:13:59.445 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} start cronjob "MySecondCronJob"
2021-11-14 12:14:00.443 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:14:01.442 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
2021-11-14 12:14:02.443 {189cwi9ngk0cfp7l8gcwciw100sr9cuu} Every second
```

## `AddSingleton`添加单例定时任务

单例定时任务，即同时只能有一个该任务正在运行。当第二个相同的定时任务触发执行时，如果发现已有该任务正在执行，第二个任务将会退出不执行，定时器将会继续等待下一次定时任务的触发检测，以此类推。可以使用 `AddSingleton` 添加单例定时任务。

```go
package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main() {
    var (
        err error
        ctx = gctx.New()
    )
    _, err = gcron.AddSingleton(ctx, "* * * * * *", func(ctx context.Context) {
        g.Log().Print(ctx, "doing")
        time.Sleep(2 * time.Second)
    })
    if err != nil {
        panic(err)
    }
    select {}
}
```

执行后，输出结果为：

```text
2021-11-14 12:16:54.073 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:16:57.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:00.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:03.071 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:06.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
2021-11-14 12:17:09.072 {189cwi9nmm0cfp7niz319fc100zrw0ig} doing
...
```

## `AddOnce`添加单次定时任务

单次定时任务， `AddOnce` 方法用于添加只运行一次的定时任务，当运行一次数后该定时任务自动销毁，`Size` 方法可以查看运行状态，相关方法：

```go
package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main()  {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddOnce(ctx, "@every 2s", func(ctx context.Context) {
        array.Append(1)
    })
    fmt.Println(cron.Size(),array.Len())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(cron.Size(),array.Len())

}
```

执行后，输出结果为：

```text
1 0
0 1
```

## `AddTimes`添加指定次数的定时任务

指定次数的定时任务， `AddTimes` 方法用于添加运行指定次数的定时任务，当运行 `times` 次数后该定时任务自动销毁， `Size` 方法可以查看运行状态，相关方法：

```go
package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 2s", 2,func(ctx context.Context) {
        array.Append(1)
    })
     fmt.Println(cron.Size(), array.Len())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(cron.Size(), array.Len())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(cron.Size(), array.Len())
}
```

执行后，输出结果为：

```text
1 0
1 1
0 2
```

## `Entries`获取注册的定时任务列表

获取所有注册的定时任务信息， `Entries` 方法用于获取当前所有已注册的定时任务信息，以切片的形式返回（按注册时间 `asc` 排序），相关方法：

```go
package main

import (
    "context"
    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/os/gcron"
    "github.com/gogf/gf/v2/os/gctx"
    "time"
)

func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 1s", 2,func(ctx context.Context) {
        array.Append(1)
    },"cron1")
    cron.AddOnce(ctx, "@every 1s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    entries := cron.Entries()
    for  k, v := range entries{
        fmt.Println(k,v.Name,v.Time)

    }
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len())

}
```

执行后，输出结果为：

```text
0 cron2 2022-02-09 10:11:47.2421345 +0800 CST m=+0.159116501
1 cron1 2022-02-09 10:11:47.2421345 +0800 CST m=+0.159116501
3
```

## `Search`搜索定时任务

`Search` 搜索返回具有指定“名称”的计划任务。(返回定时任务 `*Entry` 对象指针)，如果找不到，则返回nil。相关方法：

```go
func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 1s", 2,func(ctx context.Context) {
        array.Append(1)
    },"cron1")
    cron.AddOnce(ctx, "@every 1s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    search := cron.Search("cron2")

    g.Log().Print(ctx, search)

    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len())

    // Output:
    // 3
}

```

执行后，输出结果为：

```text
2022-02-09 10:52:30.011 {18a909957cfed11680c1b145da1ef096} {"Name":"cron2","Time":"2022-02-09T10:52:29.9972842+08:00"}
```

## `Stop`暂停定时任务

`Stop` 方法用于停止定时任务( `Stop`会停止但不会删除), 可通过 `name` 参数指定需要停止的任务名称，如果没有指定 `name`，它将停止整个 `cron`。相关方法：

```go
func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 2s", 1,func(ctx context.Context) {
        array.Append(1)
    },"cron1")
    cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    fmt.Println(array.Len(),cron.Size())
    cron.Stop("cron2")
    fmt.Println(array.Len(),cron.Size())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len(),cron.Size())

    // Output:
    // 1
    // 1
}
```

执行后，输出结果为：

```text
0 2
0 2
1 1
```

:::info
`Stop`方法会对定时任务做暂停标记，并立即返回，并不会等待正在执行的定时任务结束。但在某些场景下，我们可能需要等待当前正在执行的定时任务完成后再退出。从`v2.8`版本开始，提供了`StopGracefully`方法，用于优雅地暂停定时任务。该方法会阻塞等待正在执行的定时任务完成后再返回。
:::

## `Remove`停止并删除任务

`Remove` 方法用于根据名称 `name` 删除定时任务(停止并删除)；相关方法：

```go
func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddTimes(ctx, "@every 2s", 1,func(ctx context.Context) {
        array.Append(1)
    },"cron1")
    cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    fmt.Println(array.Len(),cron.Size())
    cron.Remove("cron2")
    fmt.Println(array.Len(),cron.Size())
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len(),cron.Size())
    // Output:
    // 0 2
    // 0 1
    // 1 0
}
```

执行后，输出结果为：

```text
0 2
0 1
1 0
```

## `Start`启动定时任务

`Start` 方法用于启动定时任务( `Add` 后自动启动定时任务), 可通过 `name` 参数指定需要启动的任务名称。如果没有指定 `name`，它将启动整个 `cron`。相关方法：

```go
func main() {
    var (
        ctx = gctx.New()
    )
    cron := gcron.New()
    array := garray.New(true)
    cron.AddOnce(ctx, "@every 2s",func(ctx context.Context) {
        array.Append(1)
    },"cron2")
    cron.Stop("cron2")
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len(),cron.Size())
    cron.Start("cron2")
    time.Sleep(3000 * time.Millisecond)
    fmt.Println(array.Len(),cron.Size())

    // Output:
    // 0 1
    // 1 0
}
```

执行后，输出结果为：

```text
0 1
1 0
```
