Skip to content

Instantly share code, notes, and snippets.

@pfrozi
Last active October 28, 2021 17:25
Show Gist options
  • Save pfrozi/10dd07c073dfebda47b6d337772507ba to your computer and use it in GitHub Desktop.
Save pfrozi/10dd07c073dfebda47b6d337772507ba to your computer and use it in GitHub Desktop.
Setting up a cron job in linux

CRONTAB SYNTAX

m h d M w /directory/command output

{m h d M w} specify the time/date and recurrence of the job

  • m(inunte) h(our) d(ay) M(onth) (day of)w(eek)
  • {/directory/command} specifies the location and comand you want to run
  • {output} is a optional segment. It defines how system notifies the user of the job completion

OPERATORS

  • Use asterisk(*) to set every time in some position. Ex.: 7 * * * * (the command will be executed every minute 7 at any hour)
  • Use comma(,) to separete individual values. Ex.: 7,14 * * * * (execute that twice per hour at minute 7 and 14)
  • Use dash(-) to indicate a range of values. Ex.: 0 8-18 * * * (execute that once per hour from 8 to 18)
  • Use forward-slash(/) to divide a value in steps. Ex.: */15 * * * * (execute that every fifteenth minute)

Example

0 0 */5 * * docker system prune -f && docker image prune -fa 
* * * * * echo TESTE_CRON >/dev/null 2>&1
@pfrozi
Copy link
Author

pfrozi commented Mar 27, 2021

@pfrozi
Copy link
Author

pfrozi commented Mar 27, 2021

docker system prune | Docker Documentation
https://docs.docker.com/engine/reference/commandline/system_prune/

docker image prune | Docker Documentation
https://docs.docker.com/engine/reference/commandline/image_prune/

@pfrozi
Copy link
Author

pfrozi commented Oct 28, 2021

go cron:

package main
import (
	"fmt"
	"time"
	"strconv"
	"github.com/go-co-op/gocron"
)
func task() {
    fmt.Println(time.Now().Format("01-02-2006 15:04:05") + " ----- " + "I am running task.")
}
func taskWithParams(id string) {
    fmt.Println(time.Now().Format("01-02-2006 15:04:05") + " ----- " + "task id : " + id)
}
func main() {
    s := gocron.NewScheduler(time.UTC)
    fmt.Println(time.Now().Format("01-02-2006 15:04:05"))
    
    s.CronWithSeconds("*/1 * * * * *").StartImmediately().Do(taskWithParams, strconv.Itoa(1))
    _, _ = s.Every(8).Second().StartImmediately().Do(taskWithParams, strconv.Itoa(2))
    
    s.StartAsync()
    time.Sleep(10 * time.Second)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment