# 合上笔记本屏幕时关闭屏幕

在设置了[合上笔记本屏幕不休眠](https://github.com/huataihuang/cloud-atlas-draft/tree/6f3204fffc11cf006abd394631e2598d98b415c3/os/linux/redhat/system_administration/systemd/disable_suspend_when_close_laptop_lid/README.md)之后，你会发现此时笔记本的屏幕也继续工作者。对于旧款的MacBook，其笔记本A面的Logo会发出刺眼的白光，并且也造成能源的无谓消耗，更带来笔记本温度升高。

此时需要进一步设置关闭屏幕。

## 图形界面关闭屏幕

* 以root身份执行以下命令

```bash
# sudo su
echo 'event=button/lid.*' | tee --append /etc/acpi/events/lm_lid
echo 'action=/etc/acpi/lid.sh' | tee --append /etc/acpi/events/lm_lid
touch /etc/acpi/lid.sh
chmod +x /etc/acpi/lid.sh
```

* 然后编辑 `/etc/accpi/lid.sh` 脚本，并且将 `your_username` 替换成你的主用户名:

```bash
#!/bin/bash

USER=your_username

grep -q close /proc/acpi/button/lid/*/state

if [ $? = 0 ]; then
  su -c  "sleep 1 && xset -display :0.0 dpms force off" - $USER
fi

grep -q open /proc/acpi/button/lid/*/state

if [ $? = 0 ]; then
  su -c  "xset -display :0 dpms force on &> /tmp/screen.lid" - $USER
fi
```

> 注意：这里使用了 `xset` 来关闭屏幕，所以实际上要求进入图形界面操作，否则会提示 `xset: unable to open display ":0.0"`

## 控制台关闭屏幕

关闭屏幕的简单命令如下:

```
sudo vbetool dpms off
```

建议采用如下方法，这样即使关闭屏幕，只要按下 `Enter` 键就能恢复

```
sudo sh -c 'vbetool dpms off; read ans; vbetool dpms on'
```

## 参考

* [Ubuntu 18.04 – Disable screen on lid close](https://mensfeld.pl/2018/08/ubuntu-18-04-disable-screen-on-lid-close/)
* [Turn off monitor using command line](https://askubuntu.com/questions/62858/turn-off-monitor-using-command-line)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://huataihuang.gitbook.io/cloud-atlas-draft/os/linux/redhat/system_administration/systemd/disable_screen_on_lid_close.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
