命令行生存技巧:强制早睡

安装

在 archlinux 上要安装 kgotobed,首先需要安装 dkms 和 linux-headers 这两个包:


  1. sudo pacman -S dkms linux-headers --noconfirm

然后去 github 上克隆它的源代码:


  1. git clone https://github.com/nikital/kgotobed.git

最后我们对源代码进行手工编译:


  1. cd kgotobed/
  2. sudo make install

make install 命令会完成以下三件事情:

  1. 通过 DKMS 注册 kgotobed.ko
  2. 安装 gotobed 命令到 /usr/local/bin 下
  3. 在 systemd 中注册、启用并运行 kgotobed.service
使用

直接运行 gotobed 就能看到设置的强行关机的时间:


  1. [vagrant@archlinux kgotobed]$ gotobed 22:00
  2. Current bedtime is 2018-04-13 01:00:00
  3. Setting bedtime to 2018-04-12 22:00:00
  4. Bedtime will be in 10 hours 32 minutes

不过你不能把时间调到更晚……


  1. [vagrant@archlinux kgotobed]$ gotobed 2:00
  2. Current bedtime is 2018-04-13 01:00:00
  3. Error: Attempting to postpone bed time, it doesn't work like this...

要想调整默认时间,你需要修改 /etc/systemd/system/kgotobed.service

原始的 kgotobed.service 内容如下所示:


  1. [Unit]
  2. Description=kgotobed: Poweroff at bedtime
  3. After=dkms.service
  4.  
  5. [Service]
  6. Type=oneshot
  7. ExecStartPre=/sbin/modprobe kgotobed
  8. Environment=TIME=01:00
  9. ExecStart=/usr/local/bin/gotobed -f ${TIME}
  10.  
  11. [Install]
  12. WantedBy=multi-user.target

我们将其中的 TIME=01:00 改成 TIME=02:00 后重启系统,再次执行 gotobed 可以看到时间已经修改了:


  1. [vagrant@archlinux ~]$ gotobed
  2. Current bedtime is 2018-04-13 02:00:00
删除 kgotobed

要逃脱强制关机的命运,几乎唯一的办法就是卸载 kgotobed 了。

我们可以在源代码目录中执行 make uninstall 进行卸载:


  1. [vagrant@archlinux ~]$ cd kgotobed/
  2. [vagrant@archlinux kgotobed]$ sudo make uninstall
  3. dkms remove kgotobed/1.0 --all
  4.  
  5. -------- Uninstall Beginning --------
  6. Module: kgotobed
  7. Version: 1.0
  8. Kernel: 4.15.15-1-ARCH (x86_64)
  9. -------------------------------------
  10.  
  11. Status: Before uninstall, this module version was ACTIVE on this kernel.
  12.  
  13. kgotobed.ko:
  14. - Uninstallation
  15. - Deleting from: /usr/lib/modules/4.15.15-1-ARCH/extra//
  16. - Original module
  17. - No original module was found for this module on this kernel.
  18. - Use the dkms install command to reinstall any previous module version.
  19.  
  20. depmod...........
  21.  
  22. DKMS: uninstall completed.
  23.  
  24. ------------------------------
  25. Deleting module version: 1.0
  26. completely from the DKMS tree.
  27. ------------------------------
  28. Done.
  29. rm -f /usr/local/bin/gotobed
  30. rm -f /etc/systemd/system/kgotobed.service
  31. systemctl daemon-reload
  32. [vagrant@archlinux kgotobed]
【声明】:芜湖站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章