Switching your monitor off remotely (linux, windows, and mac)

If you want to switch off your monitor from your command line in linux, there is an easy way:

sleep 1 && xset -display :0.0 dpms force off

What if you want to run this command remotely?

Here is my take on this question:
– use Python and Bottle to build a simple web server.
– use “/on” and “/off” pages to run command line scripts that switch the monitor on and off.

Let’s do this in Linux, Windows and Mac.

LINUX

from bottle import route, run
import os
 
@route('/off')
def off():
        os.system("xset -display :0.0 dpms force off")
        return "Screen is now off"
 
@route('/on')
def on():
        os.system("xset -display :0.0 dpms force on")
        return "Screen is now on"
 
run(host='192.168.0.xxx', port=53535)

you just run this server in the background. If you need to switch the monitor off or on, just get any machine within the network and point its browser to these links:

http://192.168.0.xxx:53535/off
http://192.168.0.xxx:53535/on

WINDOWS
To do the same in windows, you can use nircmd, a free utility, to switch off the screen.

To switch on the screen back, I’m using a simple autohotkey script, that just moves the mouse a bit:

MouseMove, 200, 100
MouseMove, 100, 100

Save the above two lines in a text file with the name mouse.ahk, in the same folder as the script below:

from bottle import route, run
import os
 
@route('/off')
def off():
        os.system("nircmd.exe monitor off")
        return "Screen is now off"
 
@route('/on')
def on():
        os.system("mouse.ahk")
        return "Screen is now on"
 
run(host='192.168.0.xxx', port=53535)

The command needs to be in your path (alternatively give it the full path of the nircmd binary).

MAC
with a bit of help from here, the script is as follows:

from bottle import route, run
import os
 
@route('/off')
def off():
        os.system("pmset displaysleepnow")
        return "Screen is now off"
 
@route('/on')
def on():
        os.system("caffeinate -u &")
        return "Screen is now on"
 
run(host='192.168.0.xxx', port=53535)

No security considerations taken into account here; we are assuming you are running this in a secure environment. Depending on your setup, you may have to use administrator/root privileges and make adjustments to your firewall.

The above is part of my IKEA mirror project. Some button combinations will be switching monitors on and off.