How could i execute the .go file using crontab in ubuntu

Hi, I’m new to golang.I just created a programming file using the below code.

package main
import (
	"database/sql"
	_ "mysql-master"
	"fmt"
)
func main() {
	db, err := sql.Open("mysql", "user:pass@/database?charset=utf8")
	checkErr(err)
	// insert
	stmt, err := db.Prepare("INSERT userinfo SET username=?,departname=?,created=?")
  checkErr(err)
  res, err := stmt.Exec("astaxie", "astaxie", "2012-12-09")
  checkErr(err)
  id, err := res.LastInsertId()
  checkErr(err)
  fmt.Println(id)
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

when I was using the command like this go run insert-data.go, it inserts a new row into the database.

How can I use the same command in ubuntu crontab?. Is it possible?
And i know that there is a proper method for cron, but I want to use it like this.

Help me please

You should build the program into an executable file. This is done by

$ go build insert-data.go

This will produce a file called insert-data that is executable. Put the complete path to this executable into your cron job.

Please also take a look at the basic document How to Write Go Code.

2 Likes

It’s working, Thank you

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.