记UbuntuServer自动关机问题的排查和解决

起因

家里的HomeAssistant、Emby、qBittorrent等服务都由一台工控机提供,跑的UbuntuServer,版本是20.04.1。
今天新买的硬盘到了开始折腾,没想到拆了个机又装上之后,运行没过多久就关机了。那个小破电源低负载的时候就会啸叫,所以工控机一停我就能知道。本来以为是电源线松了什么的,拔插了电源线,没想到过了十来分钟啸叫声又来了。这下心凉了一半:不会刚拆机的时候把啥东西搞坏了吧。
因为windows用惯了,想着会不会是蓝屏啥的,就接了个显示器,结果啥都没有,又拔插了电源线等开机,居然看见我的UbuntuServer有图形登录界面了(刚接的显示器没拔),但也没咋在意。

排查

看了看last,一看吓一跳:
kay pts/0 192.168.50.20 Sat Feb 27 17:21 - crash (00:35)
这个crash真是吓人,果真是硬件崩溃了吗?
再看看syslog,真相大白:

Feb 27 17:54:52 ubuntu kernel: [ 2069.797166] PM: suspend entry (deep)
Feb 27 17:54:52 ubuntu systemd-sleep[15356]: Suspending system...
Feb 27 17:54:52 ubuntu systemd[1]: Starting Suspend...
Feb 27 17:54:52 ubuntu systemd[1]: Reached target Sleep.
Feb 27 17:54:51 ubuntu gnome-shell[1373]: Screen lock is locked down, not locking
Feb 27 17:54:51 ubuntu NetworkManager[677]:   [1614419691.9863] manager: NetworkManager state is now ASLEEP
Feb 27 17:54:51 ubuntu NetworkManager[677]:   [1614419691.9862] manager: sleep: sleep requested (sleeping: no  enabled: yes)

原来是休眠了,虚惊一场

解决

查看休眠策略

kay@ubuntu:~$ systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target
● sleep.target - Sleep
     Loaded: loaded (/lib/systemd/system/sleep.target; static; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:systemd.special(7)

● suspend.target - Suspend
     Loaded: loaded (/lib/systemd/system/suspend.target; static; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:systemd.special(7)

● hibernate.target - Hibernate
     Loaded: loaded (/lib/systemd/system/hibernate.target; static; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:systemd.special(7)

● hybrid-sleep.target - Hybrid Suspend+Hibernate
     Loaded: loaded (/lib/systemd/system/hybrid-sleep.target; static; vendor preset: enabled)
     Active: inactive (dead)
       Docs: man:systemd.special(7)

禁用自动休眠

kay@ubuntu:~$ sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
[sudo] password for kay: 
Created symlink /etc/systemd/system/sleep.target → /dev/null.
Created symlink /etc/systemd/system/suspend.target → /dev/null.
Created symlink /etc/systemd/system/hibernate.target → /dev/null.
Created symlink /etc/systemd/system/hybrid-sleep.target → /dev/null.

再次查看休眠策略

kay@ubuntu:~$ systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target
● sleep.target
     Loaded: masked (Reason: Unit sleep.target is masked.)
     Active: inactive (dead)

● suspend.target
     Loaded: masked (Reason: Unit suspend.target is masked.)
     Active: inactive (dead)

● hibernate.target
     Loaded: masked (Reason: Unit hibernate.target is masked.)
     Active: inactive (dead)

● hybrid-sleep.target
     Loaded: masked (Reason: Unit hybrid-sleep.target is masked.)
     Active: inactive (dead)

后记

但是为啥拆个机会让自动休眠生效呢?
我又想起UbuntuServer那不该有的图形登录界面,因为如果是Desktop的话,有自动休眠就很好理解了,也就是说我的Server不知道啥时候变成Desktop了吗?

不管怎样,我的服务器又可以愉快的7x24运行了。

参考

Get more information about the crash
How To: Disable Sleep on Ubuntu Server

你可能感兴趣的:(记UbuntuServer自动关机问题的排查和解决)