Home > Backend Development > Golang > How to write a window scheduled shutdown in golang

How to write a window scheduled shutdown in golang

藏色散人
Release: 2021-02-04 14:17:04
forward
2479 people have browsed it

The following tutorial column will introduce to you how to write a window scheduled shutdown in golang. I hope it will be helpful to friends in need!

The code is as follows:

package mainimport (
    "flag"
    "fmt"
    "github.com/robfig/cron"
    "time")import (
    . "github.com/CodyGuo/win")var (
    arg string)func init() {
    flag.StringVar(&arg, "uFlags", "shutdown", "shutdown logoff reboot")}func main() {
    flag.Parse()

    c := cron.New(cron.WithSeconds())
    c.AddFunc("0 40 18 * * ?", shutdown)
    c.Start()
    select {}

    switch arg {
    case "logoff":
        logoff()
    case "reboot":
        reboot()
    case "shutdown":
        shutdown()
    default:
        fmt.Println("您输入的参数有误.")
    }}func Test(){
    fmt.Println(time.Now())}func logoff() {
    ExitWindowsEx(EWX_LOGOFF, 0)}func reboot() {
    getPrivileges()
    ExitWindowsEx(EWX_REBOOT, 0)}func shutdown() {
    getPrivileges()
    ExitWindowsEx(EWX_SHUTDOWN, 0)}func getPrivileges() {
    var hToken HANDLE    var tkp TOKEN_PRIVILEGES    OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken)
    LookupPrivilegeValueA(nil, StringToBytePtr(SE_SHUTDOWN_NAME), &tkp.Privileges[0].Luid)
    tkp.PrivilegeCount = 1
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED    AdjustTokenPrivileges(hToken, false, &tkp, 0, nil, nil)}
Copy after login
How to write a window scheduled shutdown in golangcron expression
c.AddFunc("0 40 18 * * ?", shutdown)
Copy after login

The above is the detailed content of How to write a window scheduled shutdown in golang. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
How to choose golang web mvc framework
From 1970-01-01 08:00:00
0
0
0
Is it necessary to use nginx when using golang?
From 1970-01-01 08:00:00
0
0
0
golang - vim plug-in to write go
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template