Linux系统如何配置指定时间自动关机?crontab实现方法咨询
Set Up Automatic 10 PM Shutdown on Linux with Crontab
No worries, let's break this down step by step—crontab is perfect for this, and it's straightforward once you know the syntax.
First, understand the crontab time format
Crontab uses a 5-field schedule pattern, in this order:minute hour day month weekday
- Minute: 0-59
- Hour: 0-23 (so 10 PM translates to 22)
- Day: 1-31
- Month: 1-12
- Weekday: 0-7 (0 and 7 both mean Sunday)
An asterisk * means "every" for that field—we'll use this to make the shutdown run every single day.
Step-by-step configuration
Open the crontab editor
- For your regular user account:
Note: If you hit permission errors later when the shutdown tries to run, usecrontab -esudo crontab -einstead to edit the root user's crontab (root has full access to system commands like shutdown). - If this is your first time using crontab, it'll ask you to pick an editor. Go with
nano—it's the most beginner-friendly option.
- For your regular user account:
Add the shutdown schedule
Scroll to the bottom of the editor file and paste this line:0 22 * * * /sbin/shutdown -h nowLet's unpack this:
0 22: Triggers the command at exactly 22:00 (10 PM)* * *: Runs every day, every month, every weekday/sbin/shutdown -h now: The actual shutdown command (-hmeans halt; you can swap it for-Pto power off the system—they work almost identically on most Linux distros)
Save and exit
- In nano, press
Ctrl+O, hit Enter to confirm the filename, then pressCtrl+Xto close the editor.
- In nano, press
Verify your setup
Run this command to check that your new crontab entry was saved correctly:crontab -lYou should see the 10 PM shutdown line listed in the output.
Quick extra tips
- Test before trusting: If you want to confirm the setup works without waiting till 10 PM, add a temporary test line (like triggering a shutdown 1 minute from now):
Don't forget to delete this line after testing!* * * * * /sbin/shutdown -h +1 - Cancel a pending shutdown: If you need to abort a scheduled shutdown before it runs, use:
shutdown -c
内容的提问来源于stack exchange,提问作者user869304
相关产品推荐
相关产品推荐

