#546·cron

Gracefull stop cronjob

Author: quanlndztCreated May 22, 2025Updated Dec 4, 2025

Hey team, I have problem with stop cron job. When I stop server while the job is running, it will be force close my job. I want to Gracefull stop the job Does have any solution for this?

Version

  • go 1.24.2
  • github.com/robfig/cron v1.2.0

Code sample

package main

import (
	"fmt"
	"os"
	"os/signal"
	"sync"
	"syscall"
	"time"

	"github.com/robfig/cron"
)

var (
	cronJob   *cron.Cron
	startOnce sync.Once
	stopOnce  sync.Once
)

func StartCronJobs() {
	startOnce.Do(func() {
		fmt.Println("[StartCronJobs] start cronjob")
		cronJob = cron.New()
		cronJob.Start()
		cronJob.AddFunc("@every 10s", func() {
			fmt.Println("Cron job running at", time.Now())
			TestJob()
			fmt.Println("Cron job running completed at", time.Now())
		})
	})
}

func TestJob() error {
	time.Sleep(7 * time.Second)
	return nil
}

func StopCronJobs() {
	stopOnce.Do(func() {
		fmt.Println("[StopCronJobs] stop at ", time.Now())
		cronJob.Stop()
		fmt.Println("[StopCronJobs] stop completed at", time.Now())
	})
}

func main() {
	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM)

	StartCronJobs()

	<-c
	fmt.Println("[Main] stop server")
	StopCronJobs()
	fmt.Println("[Main] stop server completed")
}

Reproduce

  • Run my code
  • When I run my code, when the job is running => Press Ctl +C to cancel the code

Result


[StartCronJobs] start cronjob
Cron job running at 2025-05-22 10:29:44.008364506 +0700 +07 m=+9.570125026
^C[Main] stop server
[StopCronJobs] stop at  2025-05-22 10:29:45.727787596 +0700 +07 m=+11.289548167
[StopCronJobs] stop completed at 2025-05-22 10:29:45.727830408 +0700 +07 m=+11.289590937
[Main] stop server completed