Ubuntu 16.04 启动缓慢(apt-daily.service)

我的电脑开机速度太慢了,我不知道为什么。
$ systemd-analyze 
Startup finished in 10.975s (kernel) + 49.732s (userspace) = 1min 708ms
$ systemd-analyze blame 
         34.971s apt-daily.service
         20.590s snapd.refresh.service
         17.113s grub-common.service
         16.033s apport.service
         16.027s networking.service
         15.894s ondemand.service
         15.860s irqbalance.service
         15.655s speech-dispatcher.service
         11.695s ModemManager.service
          9.772s accounts-daemon.service
          8.626s NetworkManager-wait-online.service
          8.058s systemd-logind.service
          8.053s bluetooth.service
          7.944s gpu-manager.service
          7.896s alsa-restore.service
          7.892s pppd-dns.service
          7.882s rsyslog.service
          7.860s avahi-daemon.service
          7.844s dev-sda1.device
          7.842s systemd-user-sessions.service
          7.648s lightdm.service
          7.610s teamviewerd.service
          6.445s apparmor.service

另外,在启动过程中,我看到一个类似这样的消息:
device descriptor read/all, error -62
ata1 softreset failed (device not ready) #most of the times
error loading journal #(sometimes)
Test WP failed, assume Write Enabled
Asking for cache data failed     #most of the times
Assuming drive cache: write through

apt-daily.service是导致启动最慢的服务。有什么办法可以解决这个问题吗?
我使用的是Ubuntu MATE 16.04。

我曾经遇到过同样的问题。你可以在这个链接中看到,我为了加快速度而去除了很多东西,结果反而引发了问题。后来我改用了Ubuntu Gnome 14.04,从那以后它一直运行得非常快,并且从55秒的启动时间缩短到了20秒。之后换成SSD后,现在只需要大约4秒就能启动了。 - Delorean
这曾经发生在我身上,但是当你第一次安装Ubuntu时,你会有选择将硬盘分区的选项。这对于内存较低的人来说也非常有用。还有其他方法。看看这个链接:https://help.ubuntu.com/stable/ubuntu-help/disk-partitions.html 我在我的硬盘中有1 GB用于引导,它运行得非常快。希望能帮到你! - schung
3个回答

这是Debian bug #844453apt-daily.service 不应该在启动时运行,而只能在之后的某个时间运行。
作为一种解决方法,请执行sudo systemctl edit apt-daily.timer并将以下文本粘贴到编辑窗口中:
# apt-daily timer configuration override
[Timer]
OnBootSec=15min
OnUnitActiveSec=1d
AccuracySec=1h
RandomizedDelaySec=30min

这将改变触发apt-daily.service在启动后随机时间在15分钟到45分钟之间运行,并且以后每天运行一次的“计时器”。请参阅systemd.timer manpage以获取更多(遗憾的是,解释不太清楚)关于这意味着什么的说明。

è؟™é€‚用ن؛ژsystemd-analyze blameه‘½ن»¤هˆ—ه‡؛çڑ„ن»»ن½•وœچهٹ،هگ—ï¼ں - Muhammad Gelbana
@MuhammadGelbana 我不明白你想问我什么。 - zwol
1我想问一下,我能否运行类似的命令,比如 sudo systemctl edit snapd.refresh.timer,并添加你在答案中发布的相同文本来覆盖 snapd.refresh 的计时器配置? - Muhammad Gelbana
1它应该适用于任何实际上是“计时器”的东西,但不适用于在启动时启动的任意服务。 - zwol
如果我想要自动化这个配置更改,有没有一种直接对文件路径进行操作而不是使用systemctl edit的方法? - openCivilisation
1@openCivilisation 是的,创建 /etc/systemd/system/apt-daily.timer.d/override.conf 文件,并将显示在那里的文本添加进去 -- 这与 systemctl edit 的功能完全一样。之后需要运行 systemctl daemon-reload 或重新启动计算机。详见 https://serverfault.com/questions/840996/modify-systemd-unit-file-without-altering-upstream-unit-file - zwol
感谢分享。为什么不需要同时更改每日升级计时器呢? - openCivilisation
@openCivilisation 你应该提出一个关于那个问题的新问题。 - zwol

另一个答案解决了问题中的第二段文字。
对我来说,要解决 apt-daily.service 永远不结束的问题,似乎这个Ubuntu论坛的帖子有效。

issue these commands as an example of how to edit /etc/systemd/system.conf

sudo cp /etc/systemd/system.conf /etc/systemd/system.conf.bak 

That will make a back-up for safety.

gksudo gedit /etc/systemd/system.conf 

Look for and change these 2 lines:

#DefaultTimeoutStartSec=90s
#DefaultTimeoutStopSec=90s 

Mine now look like this:

DefaultTimeoutStartSec=10s 
DefaultTimeoutStopSec=10s 

Been doing that for year now and nothing bad has come of it.

删除行首的禁用符号#,并更改数值。

1我无法通过这些超时启动,不得不从恢复模式下注释掉这些行。 - Yuri Gor
1@YuriGor 对不起,我猜自从16.10以后有些事情改变了。 - cat
1我的版本是18.04。我记不太清楚了,但应该是与sda相关的问题,所以可能是我的硬盘需要花费太长时间来挂载或其他什么原因。 - Yuri Gor

如果是台式机,打开它并断开然后重新连接所有硬盘驱动器电缆,包括主板。
如果这样还不行,听起来你的硬盘可能超时了。现在备份所有数据。
将Ubuntu的网络安装版本刻录到适合您系统的光盘上,启动到“尝试Ubuntu”。然后运行Gparted:默认情况下,它会检查硬盘读取错误。如果您有所有数据的备份,也许其他人可以提供建议。