Need a alarm every 20 min to get up, relax your eyes and refocus? Or maybe a cron job schedule to run every 10 mins or so? Here's a easy solution...
We will create a simple system-alert by using pc speaker to beep for few seconds as a signal. We can also trigger desktop notification. Basically, this same concept can be used to trigger any cron job as per our requirement.
This is built with GO lang so that must be installed. Next, we will import couple of libraries-
So let's begin-
go mod init beep
// name of project is 'beep'main.go
and start writing codego mod tidy
to add/remove all imported packages while writing codego run .
to run the codego build .
to save the program as an executableLet's dive into the code of main.go
file.
Firt we have all the packages imports.
package main
import (
"fmt"
"time"
"github.com/gen2brain/beeep"
"github.com/go-co-op/gocron"
)
Next we have the beep function which is our job that we want to run at scheduled times.
func beep() {
err := beeep.Beep(beeep.DefaultFreq, DefaultDuration)
if err != nil {
panic(err)
}
}
Next is the scheduler function. Here I want to run my job at every 15 min, starting from the next rounded 15 min as per the standard time. So for example if you run this at 10:21 then next rounded 15 min is 10:30 so it will start from 10:30 and then trigger at every 15 min 10:45, 11:00, 11:15 and so on...
// Function to calculate start time at next 15 min
func getStartTime() time.Time {
var startTime time.Time
roundTo, _ := time.ParseDuration("15m")
roundTime := time.Now().Round(roundTo)
if roundTime.After(time.Now()) {
startTime = roundTime
} else {
startTime = roundTime.Add(roundTo)
}
return startTime
}
// Function to run cron job every 15 min starting from the next rounded 15 min
func runCronJobs() {
s := gocron.NewScheduler(time.Now().Location())
s.Every(15).Minutes().StartAt(getStartTime()).Do(beep)
s.StartBlocking()
}
Finally call this in main function
func main() {
runCronJobs()
}
That's it!
Our scheduler, alarm system, system-alert is ready. Run this with command go run .
from the project directory.
Instead of beep sound using system speakers we can trigger desktop notifications as well if that suits well. Look at the library docs of beeep and gocron for variations as needed.
Thanks!!
- Ayush 🙂