Home  >  Article  >  System Tutorial  >  How to automatically shut down and boot up a linux system

How to automatically shut down and boot up a linux system

WBOY
WBOYforward
2024-03-01 08:00:24755browse

Don’t be an energy waster. If your computers don't need to be on, turn them off. For convenience and for computer geeks, you can configure your Linux computer to automatically wake up and shut down.

Valuable system running time

Sometimes some computers need to be turned on all the time. This situation is allowed as long as the computer running time is not exceeded. Some people pride themselves on keeping their computers up and running for long periods of time, and now we have kernel hotfixes that only require a machine shutdown if a hardware failure occurs. What I think is more practical is to save power by reducing wear and tear on moving parts and shutting down the machine when it is not needed. For example, you can wake up the backup server at a specified time, perform the backup, and then shut it down until it is ready for the next backup. Alternatively, you can set up your Internet gateway to only run at specific times. Anything that doesn't need to be running all the time can be configured to turn on when it needs to work and turn off when it's done its job.

System Hibernation

For computers that do not need to be running all the time, use root's cron scheduled task (i.e. /etc/crontab) to shut down the computer reliably. This example creates a root scheduled task to implement scheduled shutdown at 11:15 every night.

# crontab -e -u root
# m h  dom mon dow   command
15 23 * * * /sbin/shutdown -h now

The following example only runs Monday through Friday:

15 23 * * 1-5 /sbin/shutdown -h now

You can create multiple cron jobs for different dates and times. All time and date fields can be understood through the command man 5 crontab.

A quick and easy way is to use the /etc/crontab file. But this way you have to specify the user:

15 23 * * 1-5 root shutdown -h now
Automatic wake-up

Having automatic wakeup is a cool thing; most of my SUSE (SUSE Linux) colleagues are in Nuremberg, so in order to have a few hours of work time with colleagues, I have to wake up at 5 am. Get up. My computer automatically starts working at 5:30am and I just drag myself and my coffee to my desk and get started. Pressing the power button may not seem like a big deal, but at that time of day every little thing becomes a big deal.

Waking up a Linux computer may not be as reliable as shutting it down, so you may want to try a different approach. You can use remote wake-up (Wake-On-LAN), RTC wake-up, or a scheduled wake-up method set by the PC's BIOS. The reason these methods work is that when you turn off your computer, you don't actually turn it off; it's in a very low-power state and can still receive and respond to signals. It only turns off completely when you unplug the power switch.

BIOS wakeup

BIOS wake-up is the most reliable. My system's motherboard BIOS has an easy-to-use wake scheduler (Figure 1). It's just as easy for you.

linux 系统如何自动关机和开机

Figure 1: My system BIOS has an easy-to-use wake timer.

Host remote wake-up (Wake-On-LAN)

远程唤醒是仅次于 BIOS 唤醒的又一种可靠的唤醒方法。这需要你从第二台计算机发送信号到所要打开的计算机。可以使用 Arduino 或树莓派Raspberry Pi发送给基于 Linux 的路由器或者任何 Linux 计算机的唤醒信号。首先,查看系统主板 BIOS 是否支持 Wake-On-LAN ,要是支持的话,必须先启动它,因为它被默认为禁用。

然后,需要一个支持 Wake-On-LAN 的网卡;无线网卡并不支持。你需要运行 ethtool 命令查看网卡是否支持 Wake-On-LAN :

# ethtool eth0 | grep -i wake-on
        Supports Wake-on: pumbg
        Wake-on: g

这条命令输出的 “Supports Wake-on” 字段会告诉你你的网卡现在开启了哪些功能:

  • d -- 禁用
  • p -- 物理活动唤醒
  • u -- 单播消息唤醒
  • m -- 多播(组播)消息唤醒
  • b -- 广播消息唤醒
  • a -- ARP 唤醒
  • g -- 特定数据包magic packet唤醒
  • s -- 设有密码的特定数据包magic packet唤醒

ethtool 命令的 man 手册并没说清楚 p 选项的作用;这表明任何信号都会导致唤醒。然而,在我的测试中它并没有这么做。想要实现远程唤醒主机,必须支持的功能是 g —— 特定数据包magic packet唤醒,而且下面的“Wake-on” 行显示这个功能已经在启用了。如果它没有被启用,你可以通过 ethtool 命令来启用它。

# ethtool -s eth0 wol g

这条命令可能会在重启后失效,所以为了确保万无一失,你可以创建个 root 用户的定时任务(cron)在每次重启的时候来执行这条命令。

@reboot /usr/bin/ethtool -s eth0 wol g

另一个选择是最近的网络管理器Network Manager版本有一个很好的小复选框来启用 Wake-On-LAN(图 2)。

linux 系统如何自动关机和开机

图 2:启用 Wake on LAN

这里有一个可以用于设置密码的地方,但是如果你的网络接口不支持安全开机Secure On密码,它就不起作用。

现在你需要配置第二台计算机来发送唤醒信号。你并不需要 root 权限,所以你可以为你的普通用户创建 cron 任务。你需要用到的是想要唤醒的机器的网络接口和MAC地址信息。

30 08 * * * /usr/bin/wakeonlan D0:50:99:82:E7:2B 
RTC 唤醒

通过使用实时闹钟来唤醒计算机是最不可靠的方法。对于这个方法,可以参看 Wake Up Linux With an RTC Alarm Clock[1] ;对于现在的大多数发行版来说这种方法已经有点过时了。

下周继续了解更多关于使用 RTC 唤醒的方法。

通过 Linux 基金会和 edX 可以学习更多关于 Linux 的免费 Linux 入门[2]教程。

(题图:The Observatory at Delhi[3]


The above is the detailed content of How to automatically shut down and boot up a linux system. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:linuxprobe.com. If there is any infringement, please contact admin@php.cn delete