golang怎么编写一个window定时关机


本文摘自php中文网,作者藏色散人,侵删。

下面由golang教程栏目给大家介绍golang编写一个window定时关机 ,希望对需要的朋友有所帮助!

代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

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)}

cron表达式

1

c.AddFunc("0 40 18 * * ?", shutdown)

以上就是golang怎么编写一个window定时关机的详细内容,更多文章请关注木庄网络博客!!

相关阅读 >>

go - 常用签名算法的基准测试

go语言标准库之time

修改文件夹下面所有.jpg文件名demo ->欢迎加入微信交流群一起交流学习go

golang如何打包在linux上部署

08 golang引用类型——切片

golang浮点数精度丢失问题扩展包怎么解决

用 go + websocket 快速实现一个 chat 服务

golang 1.9什么时候发布

golang实战群:日志的处理机制

聊聊dubbo-go-proxy的recoveryfilter

更多相关阅读请进入《golang》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...