時候因為工作需要,我們會對電腦設置定時關機的操作。很多小伙伴可能還不知道如何設置,這里就和大家分享一下Win10電腦如何定時關機吧。
更多系統(tǒng)教程盡在小白系統(tǒng)重裝官網
http://www.958358.com/it/63182.html
系統(tǒng):win10專業(yè)版
電腦:聯(lián)想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,接著就能看到桌面提示定時關機已取消的彈窗了。
秋日生活打卡季#
原文鏈接:統(tǒng)信UOS1060設置自動關機01
hello,大家好啊,今天給大家介紹一篇如何在統(tǒng)信UOS 1060上實現(xiàn)自動關機的文章,本篇文章采用兩種方式,第一種使用的是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
請輸入密碼:
驗證成功
正在讀取軟件包列表... 完成
正在分析軟件包的依賴關系樹
正在讀取狀態(tài)信息... 完成
將會同時安裝下列軟件:
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。
(正在讀取數(shù)據(jù)庫 ... 系統(tǒng)當前共安裝有 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) 的觸發(fā)器 ...
正在處理用于 man-db (2.8.5-2) 的觸發(fā)器 ...
正在處理用于 libc-bin (2.28.23-deepin1) 的觸發(fā)器 ...
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、系統(tǒng)已經關機
我們編寫的Web項目部署之后,經常會因為需要進行配置變更或功能迭代而重啟服務,單純的kill -9 pid的方式會強制關閉進程,這樣就會導致服務端當前正在處理的請求失敗,那有沒有更優(yōu)雅的方式來實現(xiàn)關機或重啟呢?
閱讀本文需要了解一些UNIX系統(tǒng)中信號的概念,請?zhí)崆安殚嗁Y料預習。
每個信號都有一個名字和編號,這些名字都以“SIG”開頭,例如“SIGIO ”、“SIGCHLD”等等。 信號定義在signal.h頭文件中,信號名都定義為正整數(shù)。 具體的信號名稱可以使用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
復制代碼
信號的處理有三種方法,分別是:忽略、捕捉和默認動作
優(yōu)雅關機就是服務端關機命令發(fā)出后不是立即關機,而是等待當前還在處理的請求全部處理完畢后再退出程序,是一種對客戶端友好的關機方式。而執(zhí)行Ctrl+C關閉服務端時,會強制結束進程導致正在訪問的請求出現(xiàn)問題。
Go 1.8版本之后,http.Server 內置的Shutdown() 方法支持優(yōu)雅關機,如下
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
)
// @title Docker監(jiān)控服務
// @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...")
}
復制代碼
如何驗證優(yōu)雅關機的效果呢?
上面的代碼運行后會在本地的8080端口開啟一個web服務,它只注冊了一條路由/,后端服務會先sleep 5秒鐘然后才返回響應信息。
我們按下Ctrl+C時會發(fā)送syscall.SIGINT來通知程序優(yōu)雅關機,具體做法如下:
優(yōu)雅關機實現(xiàn)了,那么該如何實現(xiàn)優(yōu)雅重啟呢?
我們可以使用 fvbock/endless 來替換默認的 ListenAndServe啟動服務來實現(xiàn), 示例代碼如下:
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服務器會監(jiān)聽下列信號:
// syscall.SIGHUP,syscall.SIGUSR1,syscall.SIGUSR2,syscall.SIGINT,syscall.SIGTERM和syscall.SIGTSTP
// 接收到 SIGHUP 信號將觸發(fā)`fork/restart` 實現(xiàn)優(yōu)雅重啟(kill -1 pid會發(fā)送SIGHUP信號)
// 接收到 syscall.SIGINT或syscall.SIGTERM 信號將觸發(fā)優(yōu)雅關機
// 接收到 SIGUSR2 信號將觸發(fā)HammerTime
// SIGUSR1 和 SIGTSTP 被用來觸發(fā)一些用戶自定義的hook函數(shù)
if err :=endless.ListenAndServe(":8080", router); err!=nil{
log.Fatalf("listen: %s\n", err)
}
log.Println("Server exiting")
}
復制代碼
如何驗證優(yōu)雅重啟的效果呢?
我們通過執(zhí)行kill -1 pid命令發(fā)送syscall.SIGINT來通知程序優(yōu)雅重啟,具體做法如下:
但是需要注意的是,此時程序的PID變化了,因為endless 是通過fork子進程處理新請求,待原進程處理完當前請求后再退出的方式實現(xiàn)優(yōu)雅重啟的。所以當你的項目是使用類似supervisor的軟件管理進程時就不適用這種方式了。
if err :=rsv.ListenAndServe(); err !=nil && err !=http.ErrServerClosed {
復制代碼
// kill 默認會發(fā)送 syscall.SIGTERM 信號
// kill -2 發(fā)送 syscall.SIGINT 信號,我們常用的Ctrl+C就是觸發(fā)系統(tǒng)SIGINT信號
// kill -9 發(fā)送 syscall.SIGKILL 信號,但是不能被捕獲,所以不需要添加它
// signal.Notify把收到的 syscall.SIGINT或syscall.SIGTERM 信號轉發(fā)給quit
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) // 此處不會阻塞
<-quit // 阻塞在此,當接收到上述兩種信號時才會往下執(zhí)行
log.Println("Shutdown Server ...")
復制代碼
無論是優(yōu)雅關機還是優(yōu)雅重啟歸根結底都是通過監(jiān)聽特定系統(tǒng)信號,然后執(zhí)行一定的邏輯處理保障當前系統(tǒng)正在處理的請求被正常處理后再關閉當前進程。使用優(yōu)雅關機還是使用優(yōu)雅重啟以及怎么實現(xiàn),這就需要根據(jù)項目實際情況來決定了。
*請認真填寫需求信息,我們會在24小時內與您取得聯(lián)系。