时候因为工作需要,我们会对电脑设置定时关机的操作。很多小伙伴可能还不知道如何设置,这里就和大家分享一下Win10电脑如何定时关机吧。
更多系统教程尽在小白系统重装官网
http://www.958358.com/it/63182.html
系统:win10专业版
电脑:联想IdeaPad 710S-13ISK-IFI
1、首先通过win+r的快捷键打开运行窗口,或者我们也可以直接在搜索栏里搜索运行。
2、然后在窗口中输入以下命令“shutdown -s -t ”,在t后面输入想要定时关机的时间,这里必须要用秒为单位的时间,比如想要一分钟后关机,那么就输入“shutdown -s -t 60”,一小时的画就是“shutdown -s -t3600”
3、在输入好命令之后,点击确定或者直接按回车,然后电脑右下角就会弹出定时关机的提示了。
4、如果我们需要固定一个时间点来关机的话,那么需要输入at 时间 shutdown -s的命令,假如你想要在晚上十点关机,那么就输入at 22:00 shutdown -s。
5、这种关机方法电脑就不会有弹窗提示了,如果你突然不想关机了,那么就再次打开运行窗口,在运行窗口里输入shutdown -a,接着就能看到桌面提示定时关机已取消的弹窗了。
秋日生活打卡季#
原文链接:统信UOS1060设置自动关机01
hello,大家好啊,今天给大家介绍一篇如何在统信UOS 1060上实现自动关机的文章,本篇文章采用两种方式,第一种使用的是crontab定时任务的方式,第二种是使用at命令的方式,两种方式供您选择。
方法一:使用crontab定时任务
1、编辑crontab文件,设置每天14:09关机
uos@uos-PC:~/Desktop$ sudo -i
请输入密码:
验证成功
root@uos-PC:~# crontab -e
no crontab for root - using an empty one
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
Choose 1-3 [1]: 2
crontab: installing new crontab
root@uos-PC:~#
root@uos-PC:~# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
09 14 * * * /sbin/poweroff
root@uos-PC:~#
2、等到14:09设备关机
方法二:使用at命令来设置一次性的关机任务
1、安装at命令
uos@uos-PC:~/Desktop$ sudo apt install at -y
请输入密码:
验证成功
正在读取软件包列表... 完成
正在分析软件包的依赖关系树
正在读取状态信息... 完成
将会同时安装下列软件:
exim4-base exim4-config exim4-daemon-light guile-2.2-libs libfl2 libgsasl7 libkyotocabinet16v5
libmailutils5 libntlm0 mailutils mailutils-common
建议安装:
exim4-doc-html | exim4-doc-info eximon4 spf-tools-perl swaks mailutils-mh mailutils-doc
下列【新】软件包将被安装:
at exim4-base exim4-config exim4-daemon-light guile-2.2-libs libfl2 libgsasl7 libkyotocabinet16v5
libmailutils5 libntlm0 mailutils mailutils-common
升级了 0 个软件包,新安装了 12 个软件包,要卸载 0 个软件包,有 116 个软件包未被升级。
需要下载 9,660 kB 的归档。
解压缩后会消耗 56.2 MB 的额外空间。
获取:1 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 libfl2 arm64 2.6.4-6.2 [103 kB]
获取:2 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 at arm64 3.1.23-1 [48.0 kB]
获取:3 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 exim4-config all 4.92.3-8+deb10u7 [325 kB]
获取:4 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 exim4-base arm64 4.92.3-8+deb10u7 [1,136 kB]
获取:5 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 exim4-daemon-light arm64 4.92.3-8+deb10u7 [533 kB]
获取:6 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 guile-2.2-libs arm64 2.2.4+1-2+deb10u1 [4,941 kB]
获取:7 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 libntlm0 arm64 1.5-1+deb10u1+rebuild [23.6 kB]
获取:8 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 libgsasl7 arm64 1.8.0-8+b2 [198 kB]
获取:9 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 libkyotocabinet16v5 arm64 1.2.76-4.2+b1 [267 kB]
获取:10 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 mailutils-common all 1:3.5-4+rebuild [689 kB]
获取:11 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 libmailutils5 arm64 1:3.5-4+rebuild [829 kB]
获取:12 https://professional-packages.chinauos.com/desktop-professional eagle/main arm64 mailutils arm64 1:3.5-4+rebuild [567 kB]
已下载 9,660 kB,耗时 9秒 (1,022 kB/s)
正在预设定软件包 ...
正在选中未选择的软件包 libfl2:arm64。
(正在读取数据库 ... 系统当前共安装有 207780 个文件和目录。)
准备解压 .../00-libfl2_2.6.4-6.2_arm64.deb ...
正在解压 libfl2:arm64 (2.6.4-6.2) ...
/var/cache/apt/archives/libfl2_2.6.4-6.2_arm64.deb
正在选中未选择的软件包 at。
准备解压 .../01-at_3.1.23-1_arm64.deb ...
正在解压 at (3.1.23-1) ...
/var/cache/apt/archives/at_3.1.23-1_arm64.deb
正在选中未选择的软件包 exim4-config。
准备解压 .../02-exim4-config_4.92.3-8+deb10u7_all.deb ...
正在解压 exim4-config (4.92.3-8+deb10u7) ...
/var/cache/apt/archives/exim4-config_4.92.3-8+deb10u7_all.deb
正在选中未选择的软件包 exim4-base。
准备解压 .../03-exim4-base_4.92.3-8+deb10u7_arm64.deb ...
正在解压 exim4-base (4.92.3-8+deb10u7) ...
/var/cache/apt/archives/exim4-base_4.92.3-8+deb10u7_arm64.deb
正在选中未选择的软件包 exim4-daemon-light。
准备解压 .../04-exim4-daemon-light_4.92.3-8+deb10u7_arm64.deb ...
正在解压 exim4-daemon-light (4.92.3-8+deb10u7) ...
/var/cache/apt/archives/exim4-daemon-light_4.92.3-8+deb10u7_arm64.deb
正在选中未选择的软件包 guile-2.2-libs:arm64。
准备解压 .../05-guile-2.2-libs_2.2.4+1-2+deb10u1_arm64.deb ...
正在解压 guile-2.2-libs:arm64 (2.2.4+1-2+deb10u1) ...
/var/cache/apt/archives/guile-2.2-libs_2.2.4+1-2+deb10u1_arm64.deb
正在选中未选择的软件包 libntlm0:arm64。
准备解压 .../06-libntlm0_1.5-1+deb10u1+rebuild_arm64.deb ...
正在解压 libntlm0:arm64 (1.5-1+deb10u1+rebuild) ...
/var/cache/apt/archives/libntlm0_1.5-1+deb10u1+rebuild_arm64.deb
正在选中未选择的软件包 libgsasl7。
准备解压 .../07-libgsasl7_1.8.0-8+b2_arm64.deb ...
正在解压 libgsasl7 (1.8.0-8+b2) ...
/var/cache/apt/archives/libgsasl7_1.8.0-8+b2_arm64.deb
正在选中未选择的软件包 libkyotocabinet16v5:arm64。
准备解压 .../08-libkyotocabinet16v5_1.2.76-4.2+b1_arm64.deb ...
正在解压 libkyotocabinet16v5:arm64 (1.2.76-4.2+b1) ...
/var/cache/apt/archives/libkyotocabinet16v5_1.2.76-4.2+b1_arm64.deb
正在选中未选择的软件包 mailutils-common。
准备解压 .../09-mailutils-common_1%3a3.5-4+rebuild_all.deb ...
正在解压 mailutils-common (1:3.5-4+rebuild) ...
/var/cache/apt/archives/mailutils-common_1%3a3.5-4+rebuild_all.deb
正在选中未选择的软件包 libmailutils5:arm64。
准备解压 .../10-libmailutils5_1%3a3.5-4+rebuild_arm64.deb ...
正在解压 libmailutils5:arm64 (1:3.5-4+rebuild) ...
/var/cache/apt/archives/libmailutils5_1%3a3.5-4+rebuild_arm64.deb
正在选中未选择的软件包 mailutils。
准备解压 .../11-mailutils_1%3a3.5-4+rebuild_arm64.deb ...
正在解压 mailutils (1:3.5-4+rebuild) ...
/var/cache/apt/archives/mailutils_1%3a3.5-4+rebuild_arm64.deb
正在设置 libkyotocabinet16v5:arm64 (1.2.76-4.2+b1) ...
正在设置 libntlm0:arm64 (1.5-1+deb10u1+rebuild) ...
正在设置 mailutils-common (1:3.5-4+rebuild) ...
正在设置 libfl2:arm64 (2.6.4-6.2) ...
正在设置 exim4-config (4.92.3-8+deb10u7) ...
Adding system-user for exim (v4)
正在设置 guile-2.2-libs:arm64 (2.2.4+1-2+deb10u1) ...
正在设置 exim4-base (4.92.3-8+deb10u7) ...
exim: DB upgrade, deleting hints-db
正在设置 at (3.1.23-1) ...
Created symlink /etc/systemd/system/multi-user.target.wants/atd.service → /lib/systemd/system/atd.service.
正在设置 libgsasl7 (1.8.0-8+b2) ...
正在设置 exim4-daemon-light (4.92.3-8+deb10u7) ...
Initializing GnuTLS DH parameter file
正在设置 libmailutils5:arm64 (1:3.5-4+rebuild) ...
正在设置 mailutils (1:3.5-4+rebuild) ...
update-alternatives: 使用 /usr/bin/frm.mailutils 来在自动模式中提供 /usr/bin/frm (frm)
update-alternatives: 使用 /usr/bin/from.mailutils 来在自动模式中提供 /usr/bin/from (from)
update-alternatives: 使用 /usr/bin/messages.mailutils 来在自动模式中提供 /usr/bin/messages (messages)
update-alternatives: 使用 /usr/bin/movemail.mailutils 来在自动模式中提供 /usr/bin/movemail (movemail)
update-alternatives: 使用 /usr/bin/readmsg.mailutils 来在自动模式中提供 /usr/bin/readmsg (readmsg)
update-alternatives: 使用 /usr/bin/dotlock.mailutils 来在自动模式中提供 /usr/bin/dotlock (dotlock)
update-alternatives: 使用 /usr/bin/mail.mailutils 来在自动模式中提供 /usr/bin/mailx (mailx)
正在处理用于 systemd (241.52-deepin1) 的触发器 ...
正在处理用于 man-db (2.8.5-2) 的触发器 ...
正在处理用于 libc-bin (2.28.23-deepin1) 的触发器 ...
uos@uos-PC:~/Desktop$
2、输入at命令,在指定时间关机
uos@uos-PC:~/Desktop$ sudo -i
请输入密码:
验证成功
root@uos-PC:~#
root@uos-PC:~# echo "poweroff" | at 14:25
warning: commands will be executed using /bin/sh
job 3 at Thu Sep 28 14:25:00 2023
root@uos-PC:~#
3、系统已经关机
我们编写的Web项目部署之后,经常会因为需要进行配置变更或功能迭代而重启服务,单纯的kill -9 pid的方式会强制关闭进程,这样就会导致服务端当前正在处理的请求失败,那有没有更优雅的方式来实现关机或重启呢?
阅读本文需要了解一些UNIX系统中信号的概念,请提前查阅资料预习。
每个信号都有一个名字和编号,这些名字都以“SIG”开头,例如“SIGIO ”、“SIGCHLD”等等。 信号定义在signal.h头文件中,信号名都定义为正整数。 具体的信号名称可以使用kill -l来查看信号的名字以及序号,信号是从1开始编号的,不存在0号信号。kill对于信号0又特殊的应用。
root@1204nStrive:~# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
复制代码
信号的处理有三种方法,分别是:忽略、捕捉和默认动作
优雅关机就是服务端关机命令发出后不是立即关机,而是等待当前还在处理的请求全部处理完毕后再退出程序,是一种对客户端友好的关机方式。而执行Ctrl+C关闭服务端时,会强制结束进程导致正在访问的请求出现问题。
Go 1.8版本之后,http.Server 内置的Shutdown() 方法支持优雅关机,如下
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
)
// @title Docker监控服务
// @version 1.0
// @description gin shutdown
// @contact.name API Support
// @contact.url http://www.swagger.io/support
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
// @host 127.0.0.1:9009
// @BasePath
func main() {
r :=gin.Default()
r.GET("/", func(c *gin.Context) {
time.Sleep(5 * time.Second)
c.String(http.StatusOK, "gin %s", "ok")
})
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
server :=http.Server{
Addr: ":8080",
Handler: r,
}
go func() {
if err :=server.ListenAndServe(); err !=nil && err !=http.ErrServerClosed {
log.Fatal("server listen err:%s", err)
}
}()
quit :=make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
// 在此阻塞
<-quit
ctx, channel :=context.WithTimeout(context.Background(), 5*time.Second)
defer channel()
if err :=server.Shutdown(ctx); err !=nil {
log.Fatal("server shutdown error")
}
log.Println("server exiting...")
}
复制代码
如何验证优雅关机的效果呢?
上面的代码运行后会在本地的8080端口开启一个web服务,它只注册了一条路由/,后端服务会先sleep 5秒钟然后才返回响应信息。
我们按下Ctrl+C时会发送syscall.SIGINT来通知程序优雅关机,具体做法如下:
优雅关机实现了,那么该如何实现优雅重启呢?
我们可以使用 fvbock/endless 来替换默认的 ListenAndServe启动服务来实现, 示例代码如下:
package main
import (
"log"
"net/http"
"time"
"github.com/fvbock/endless"
"github.com/gin-gonic/gin"
)
func main() {
router :=gin.Default()
router.GET("/", func(c *gin.Context) {
time.Sleep(5 * time.Second)
c.String(http.StatusOK, "hello gin!")
})
// 默认endless服务器会监听下列信号:
// syscall.SIGHUP,syscall.SIGUSR1,syscall.SIGUSR2,syscall.SIGINT,syscall.SIGTERM和syscall.SIGTSTP
// 接收到 SIGHUP 信号将触发`fork/restart` 实现优雅重启(kill -1 pid会发送SIGHUP信号)
// 接收到 syscall.SIGINT或syscall.SIGTERM 信号将触发优雅关机
// 接收到 SIGUSR2 信号将触发HammerTime
// SIGUSR1 和 SIGTSTP 被用来触发一些用户自定义的hook函数
if err :=endless.ListenAndServe(":8080", router); err!=nil{
log.Fatalf("listen: %s\n", err)
}
log.Println("Server exiting")
}
复制代码
如何验证优雅重启的效果呢?
我们通过执行kill -1 pid命令发送syscall.SIGINT来通知程序优雅重启,具体做法如下:
但是需要注意的是,此时程序的PID变化了,因为endless 是通过fork子进程处理新请求,待原进程处理完当前请求后再退出的方式实现优雅重启的。所以当你的项目是使用类似supervisor的软件管理进程时就不适用这种方式了。
if err :=rsv.ListenAndServe(); err !=nil && err !=http.ErrServerClosed {
复制代码
// kill 默认会发送 syscall.SIGTERM 信号
// kill -2 发送 syscall.SIGINT 信号,我们常用的Ctrl+C就是触发系统SIGINT信号
// kill -9 发送 syscall.SIGKILL 信号,但是不能被捕获,所以不需要添加它
// signal.Notify把收到的 syscall.SIGINT或syscall.SIGTERM 信号转发给quit
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) // 此处不会阻塞
<-quit // 阻塞在此,当接收到上述两种信号时才会往下执行
log.Println("Shutdown Server ...")
复制代码
无论是优雅关机还是优雅重启归根结底都是通过监听特定系统信号,然后执行一定的逻辑处理保障当前系统正在处理的请求被正常处理后再关闭当前进程。使用优雅关机还是使用优雅重启以及怎么实现,这就需要根据项目实际情况来决定了。
*请认真填写需求信息,我们会在24小时内与您取得联系。