Golang Scheduled Tasks and Windows under Service installation, removal

Source: Internet
Author: User
Tags sprintf
This is a creation in Article, where the information may have evolved or changed.

Site started using the WIN server System scheduled task, add Xx.bat, set the execution time

Content in Xx.bat:

C:\php\php.exe-q C:\WWW\api\script\AutoCancelOrder.php

This way is now working, and recently after watching Golang, I intend to use Golang to re-schedule the task.

Cron.go code below, go run requires go get-u github.com/jakecoffman/cron

Package Main

Import (
"FMT"
"Github.com/jakecoffman/cron"
"Io/ioutil"
"Net/http"
"Net/url"
"OS"
"Time"
)

Func Main () {
var C = cron. New ()
Spec: = "0 "
C.addfunc (Spec, func () {
Autotongji ()
}, "Autotongji")

spec = "*/15 * 9-17 * * ?"c.AddFunc(spec, func() {    autoFddsign()}, "autoFddsign")c.Start()select {}

}

Daily data Statistics 0:30 executed once
Func Autotongji () {
LOG: = Fmt. Sprintf ("--tongji Execution time:%s, \ n", Times.) Now (). Format ("2006-01-02 15:04:05"))
Writelog (log)
}

The law of the Big Seal
Func autofddsign () {
Log1: = Fmt. Sprintf ("--fddsign Execution time:%s, \ n", Times.) Now (). Format ("2006-01-02 15:04:05"))
Writelog (LOG1)
}

Func request (Request string) (Rtn string) {
U, : = URL. Parse (Request)
Q: = U.query ()
Q.set ("token", "site@ @crontab")
U.rawquery = Q.encode ()
Reponse,
: = http. Get (u.string ())
result, _: = Ioutil. ReadAll (reponse. Body)
Reponse. Body.close ()
RESULT_STR: = string (Result)
Return RESULT_STR
}

Write log
Func writelog (log string) {
LogDir: = "Log"

var path stringif os.IsPathSeparator('\\') {    path = "\\"} else {    path = "/"}dir, _ := os.Getwd()os.Mkdir(dir+path+logdir, os.ModePerm)file_path := dir + path + logdir + path + time.Now().Format("2006-01") + ".txt"_, err := os.Stat(file_path)if err != nil {    f, _ := os.Create(file_path)    defer f.Close()}f, _ := os.OpenFile(file_path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0644)f.WriteString(log)f.Close()

}

Func Checkerr (err error) {
If err! = Nil {
Fmt. Println (Err. Error ())
Panic (ERR)
}
}
Run Results View:


Tim20170602091555.jpg

Step two using NSSM to install and remove services
Install.bat installation Services, where absolute paths are used

NSSM Install Ddauto F:\golang\src\cron.exe
NSSM Start Ddauto

Delete.bat: Deleting a service
NSSM Stop Ddauto
NSSM Remove Ddauto

The code is a little distorted. Look at this ...

As for spec = "/15 9-17 ?" Time settings, you can refer to the use of the Linux crontab, Golang cron more than crontab units per second unit settings

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.