下面由golang教程栏目给大家介绍golang编写一个window定时关机 ,希望对需要的朋友有所帮助!
代码如下:
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表达式
c.AddFunc("0 40 18 * * ?", shutdown)
以上就是golang怎么编写一个window定时关机的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号