Maison > développement back-end > Golang > Comment écrire un arrêt programmé de fenêtre dans Golang

Comment écrire un arrêt programmé de fenêtre dans Golang

藏色散人
Libérer: 2021-02-04 14:17:04
avant
2519 Les gens l'ont consulté
Ce qui suit est introduit par

Golang La colonne tutoriel présentera Golang à l'écriture d'un arrêt de l'heure de fenêtre, j'espère que cela sera utile aux amis dans le besoin !

Comment écrire un arrêt programmé de fenêtre dans Golang

Le code est le suivant :

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

Copier après la connexion
expression cron

1

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

Copier après la connexion

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal