Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to enable and disable the HDMI signal of the Raspberry PI
#!/bin/bash -e
# /usr/local/sbin/raspi-monitor
# Script to enable and disable the HDMI signal of the Raspberry PI
# Inspiration: http://www.raspberrypi.org/forums/viewtopic.php?t=16472&p=176258
CMD="$1"
function on {
/opt/vc/bin/tvservice --preferred
# Hack to enable virtual terminal nr 7 again:
chvt 6
chvt 7
}
function off {
/opt/vc/bin/tvservice --off
}
function must_be_root {
if [ $USER != root ]; then
echo "ERROR: Script must be executed as the root user"
exit 1
fi
}
function main {
must_be_root
if [ "$CMD" == "on" ]; then
on
elif [ "$CMD" == "off" ]; then
off
else
echo "Usage: $0 <on|off>"
exit 1
fi
exit 0
}
main
# /etc/cron.d/raspi-monitor-scheduler
# Enable the monitor every weekday morning at 8:00
0 8 * * 1,2,3,4,5 root /usr/local/sbin/raspi-monitor on > /dev/null 2>&1
# Disable the monitor every weekday evening at 17:30
30 17 * * 1,2,3,4,5 root /usr/local/sbin/raspi-monitor off > /dev/null 2>&1
@rodripf

This comment has been minimized.

Show comment
Hide comment
@rodripf

rodripf Jul 18, 2015

It worked perfectly! Thanks for sharing!

rodripf commented Jul 18, 2015

It worked perfectly! Thanks for sharing!

@patricio272

This comment has been minimized.

Show comment
Hide comment
@patricio272

patricio272 Mar 31, 2016

Thanks a lot. Is there any way to remove or hide the "No Signal" message on tv?
Thanks in advance
patriciotoledochamorro@gmail.com

patricio272 commented Mar 31, 2016

Thanks a lot. Is there any way to remove or hide the "No Signal" message on tv?
Thanks in advance
patriciotoledochamorro@gmail.com

@toehold

This comment has been minimized.

Show comment
Hide comment
@toehold

toehold Apr 11, 2016

My graphical Display is on 2, so I had to change chvt 7 to chvt 2!

toehold commented Apr 11, 2016

My graphical Display is on 2, so I had to change chvt 7 to chvt 2!

@antonlacon

This comment has been minimized.

Show comment
Hide comment
@antonlacon

antonlacon May 22, 2016

You can get your current VT with fgconsole. This would let you check and be sure to change to a different one, as well as change back to the original VT.

antonlacon commented May 22, 2016

You can get your current VT with fgconsole. This would let you check and be sure to change to a different one, as well as change back to the original VT.

@sinrig

This comment has been minimized.

Show comment
Hide comment
@sinrig

sinrig Mar 18, 2017

If you'd like to use a Python script to turn off an HDMI-connected monitor when the screensaver "blanks" the screen, this one seems to work on my Raspberry Pi 3. To run this script at login, you can put it script in a file in your .local/bin directory and add a line to your lxsession autostart file, e.g.:
in file:
/home/pi/.config/lxsession/LXDE-pi
add:
@python /home/pi/.local/bin/monitor.py

Assumption: xscreensaver is installed.

---- begin Python script ----

# This program monitors the screensaver and turns the monitor off
# when the screensaver blanks the screen, and back on again when
# the screensaver unblanks the screen.

import sys  # for exit()
import subprocess  # for command execution

# command to monitor the screensaver
MONITOR = "xscreensaver-command"
MONITOR_ARGS = [MONITOR, "-watch"]
# commands to turn the screen on and off
CONTROL = "vcgencmd"
CONTROL_BLANK = [CONTROL, "display_power", "0"]
CONTROL_UNBLANK = [CONTROL, "display_power", "1"]

def main():
	# start the screen monitoring program, pipe output back to us
	prog = subprocess.Popen(MONITOR_ARGS, stdout=subprocess.PIPE)

	try:  # use try/finally to clean up if an exception occurs
		while prog.poll() is None:  # subprocess still running
			# read a line from the monitoring program, 1 char at a time
			line = char = ""
			while char != "\n":
				line += char
				char = prog.stdout.read(1)
			# here we have a complete line; check the screensaver op
			if line.startswith("BLANK "):
				subprocess.call(CONTROL_BLANK)
			elif line.startswith("UNBLANK "):
				subprocess.call(CONTROL_UNBLANK)
		return prog.returncode

	finally:  # clean up
		prog.stdout.close()
		prog.kill()

sys.exit(main())

---- end Python script ----

sinrig commented Mar 18, 2017

If you'd like to use a Python script to turn off an HDMI-connected monitor when the screensaver "blanks" the screen, this one seems to work on my Raspberry Pi 3. To run this script at login, you can put it script in a file in your .local/bin directory and add a line to your lxsession autostart file, e.g.:
in file:
/home/pi/.config/lxsession/LXDE-pi
add:
@python /home/pi/.local/bin/monitor.py

Assumption: xscreensaver is installed.

---- begin Python script ----

# This program monitors the screensaver and turns the monitor off
# when the screensaver blanks the screen, and back on again when
# the screensaver unblanks the screen.

import sys  # for exit()
import subprocess  # for command execution

# command to monitor the screensaver
MONITOR = "xscreensaver-command"
MONITOR_ARGS = [MONITOR, "-watch"]
# commands to turn the screen on and off
CONTROL = "vcgencmd"
CONTROL_BLANK = [CONTROL, "display_power", "0"]
CONTROL_UNBLANK = [CONTROL, "display_power", "1"]

def main():
	# start the screen monitoring program, pipe output back to us
	prog = subprocess.Popen(MONITOR_ARGS, stdout=subprocess.PIPE)

	try:  # use try/finally to clean up if an exception occurs
		while prog.poll() is None:  # subprocess still running
			# read a line from the monitoring program, 1 char at a time
			line = char = ""
			while char != "\n":
				line += char
				char = prog.stdout.read(1)
			# here we have a complete line; check the screensaver op
			if line.startswith("BLANK "):
				subprocess.call(CONTROL_BLANK)
			elif line.startswith("UNBLANK "):
				subprocess.call(CONTROL_UNBLANK)
		return prog.returncode

	finally:  # clean up
		prog.stdout.close()
		prog.kill()

sys.exit(main())

---- end Python script ----
@colindean

This comment has been minimized.

Show comment
Hide comment
@colindean

colindean Jul 24, 2017

Apparently, this is the better way to turn off and turn on the display power now:

sudo vcgencmd display_power 0
sudo vcgencmd display_power 1

This is reflected in @sinrig's post but it's hard to spot.

colindean commented Jul 24, 2017

Apparently, this is the better way to turn off and turn on the display power now:

sudo vcgencmd display_power 0
sudo vcgencmd display_power 1

This is reflected in @sinrig's post but it's hard to spot.

@matheusft

This comment has been minimized.

Show comment
Hide comment
@matheusft

matheusft Jan 27, 2018

Is there any way of forcing the HDMI to only display a Framebuffer from a certain virtual console and block the Raspberry from switching automatically to the virtual console 1 when something is typed in the keyboard?

matheusft commented Jan 27, 2018

Is there any way of forcing the HDMI to only display a Framebuffer from a certain virtual console and block the Raspberry from switching automatically to the virtual console 1 when something is typed in the keyboard?

@lucvdv

This comment has been minimized.

Show comment
Hide comment
@lucvdv

lucvdv Oct 6, 2018

Sinrig's solution works fine, with a couple of minuses.

  1. the xscreensave-command subprocess doesn't stop when I log out, so neither does the script.
  2. it consumes 100% CPU on one core, per instance running (i.e. one per time you logged off and back on).

I included a line 'time.sleep(0.5)' in the loop (and ' import time' near the top) to prevent 2, but haven't found a solution for 1 yet. Not that it happens a lot that I log off, usually power down or reboot.

--Edit

I have to take my words back on 1, it stopped now. Earlier today I found I had 3 copies running at 100% CPU and a thermometer warning icon at the right hand top of my desktop after logging off and back on a couple of times, can't explain why.

lucvdv commented Oct 6, 2018

Sinrig's solution works fine, with a couple of minuses.

  1. the xscreensave-command subprocess doesn't stop when I log out, so neither does the script.
  2. it consumes 100% CPU on one core, per instance running (i.e. one per time you logged off and back on).

I included a line 'time.sleep(0.5)' in the loop (and ' import time' near the top) to prevent 2, but haven't found a solution for 1 yet. Not that it happens a lot that I log off, usually power down or reboot.

--Edit

I have to take my words back on 1, it stopped now. Earlier today I found I had 3 copies running at 100% CPU and a thermometer warning icon at the right hand top of my desktop after logging off and back on a couple of times, can't explain why.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment