构思想法:
由于go语言在window环境下,可以编译成exe可以执行文件,再通过nssm工具打包成系统服务,就可以做到系统自启动后,定时服务可以自动执行
编译成exe文件密令(依赖go原生编译环境)
go build <文件名.go>
把exe打包成系统服务参考连接
https://blog.csdn.net/canduecho/article/details/80060000?utm_medium=distribute.pc_relevant_t0.none-task-blog-OPENSEARCH-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-OPENSEARCH-1.control
以下就是整体解决代码,不过存在一定小问题,就是系统服务重新启动,定时会重新计算,所以定时不是很准确
感谢,csdn的一位大佬的博客参考,我才弄出来
package main
import (
"time"
"os"
"fmt"
"io"
"path/filepath"
"log"
"syscall"
"runtime"
"strings"
)
func main() {
//开始定时任务
startJob()
//copyFile("D:/kotlin学习书籍/test/*", "D:/kotlin_test");
}
func startJob(){
//每7天就执行一次,数据迁移
t1 := time.NewTicker(time.Hour * 168)
//t1 := time.NewTicker(time.Second * 3)
//半年清除一次数据
t2 := time.NewTicker(time.Hour * 3600)
//t2 := time.NewTicker(time.Second * 3)
go executeJob(t1, t2)
select {}
}
func executeJob(t1 interface{}, t2 interface{}) {
//sourceUrl := "D:/kotlin学习书籍/test/*"
sourceUrl := "C:/Windows/System32/winevt/Logs/*";
println("sourceUrl:",sourceUrl)
//targetUrl := "D:/kotlin_test"
targetUrl := "E:/系统事件日志"
println("targetUrl:",targetUrl)
for {
select {
case <-t1.(*time.Ticker).C:
println("执行计划任务-拷贝日志")
copyFile(sourceUrl, targetUrl);
case <-t2.(*time.Ticker).C:
println("执行计划任务-删除日志")
deleteFileByDir(targetUrl+"/*")
}
}
}
func copyFile(sourceUrl string, targeUrl string)(bool){
filepathNames, err := filepath.Glob(sourceUrl)
if err != nil {
log.Fatal(err)
}
list := make([]string, 800)
for i := range filepathNames {
osType := runtime.GOOS
fileInfo, _ := os.Stat(filepathNames[i])
if osType == "windows" {
wFileSys := fileInfo.Sys().(*syscall.Win32FileAttributeData)
tNanSeconds := wFileSys.LastWriteTime.Nanoseconds() /// 返回的是纳秒
tSec := tNanSeconds/1e9 //秒
currentTime := time.Now().Unix() //秒
//如果相差天数大于7天以上就进行拷贝
distanceDay := (currentTime - tSec) / (60 * 60 * 24);
if(distanceDay >= 7 ){
fullFilename := filepathNames[i]
index := strings.LastIndex(fullFilename, "\\")
//fmt.Println(filepathNames[i], tSec) //打印path
/* filenameWithSuffix := path.Base(fullFilename)
println(filenameWithSuffix)
fileSuffix := path.Ext(filenameWithSuffix)
println(fileSuffix)
filenameOnly := strings.TrimSuffix(filenameWithSuffix, fileSuffix)
println(filenameOnly)*/
println(fullFilename)
result, _ := copy(filepathNames[i], targeUrl+fullFilename[index:])
if(result > 0){
t := append(list[i:], fullFilename)
println("append", t)
}
}
}
}
//复制完成后,删除原来文件
deleteFile(list)
return true;
}
func deleteFileByDir(url string){
filepathNames, err := filepath.Glob(url)
if err != nil {
log.Fatal(err)
}
for i := range filepathNames {
println("删除:",filepathNames[i])
os.Remove(filepathNames[i])
}
}
func deleteFile(arr []string){
for index, value := range arr {
if(len(value) > 0){
fmt.Printf("index: %d value: %d\n", index, value)
println("删除:",value)
os.Remove(value)
}
}
}
func copy(src, dst string) (int64, error) {
// print(dst)
sourceFileStat, err := os.Stat(src)
if err != nil {
return 0, err
}
if !sourceFileStat.Mode().IsRegular() {
return 0, fmt.Errorf("%s is not a regular file", src)
}
source, err := os.Open(src)
if err != nil {
return 0, err
}
defer source.Close()
println(dst)
destination, err := os.Create(dst)
if err != nil {
return 0, err
}
defer destination.Close()
nBytes, err := io.Copy(destination, source)
return nBytes, err
}