5

I have the following line to turn my laptop display off:

sleep 1 && xset dpms force standby

sleep 1: Puts the thread to sleep for 1 second.

xset dpms force standby: From man, forces the dpms flag to standby.

When I omit the sleep 1 command, the xset command puts the pc in standby mode for 1 second, then automatically resumes normal operation. But when combined with sleep 1 command, it puts the pc in standby mode indefinitely, until a user input has been detected.

Can you explain the relationship between these two commands and why it works this way?

Ravexina
  • 57,256

2 Answers2

6

When you run your command, two events happens, "KeyPress" and "KeyRelease".

So when you run this command with pressing Enter button.

xset dpms force standby

First a "KeyPress" happens, it will cause the command to immediately be run and puts the monitor to sleep, when you release the Enter key, then "KeyRelease" happens and it's a user activity so monitor comes back to working.

If you do the job really fast (Hitting the Enter and realsing it before command being run) your monitor stays at "sleep" state.


To test this things, install x11-xserver-utils package.

sudo apt install x11-xserver-utils

then run xev.

Now hit the Enter to see whats happens; you should see something more or less like:

KeyPress event, serial 48, synthetic NO, window 0x2600001,
...

KeyRelease event, serial 48, synthetic NO, window 0x2600001,
...

So we use sleep to make sure both this events happens before command being run.

Ravexina
  • 57,256
0

I think its probably to do with what the system considers to be "user activity".

If the system detects keyboard activity it comes back from sleep. That fact that you press enter to send the force standby command counts as some "current" activity and so it immediately un-sleeps.

sleep 1 ensures that the keyboard has not recorded activity for 1 second before the sleep command is sent, so standby goes ahead.

teknopaul
  • 2,137