Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enable and disable the HDMI port on the Raspberry Pi: `rpi-hdmi on` to turn on, `rpi-hdmi off` to turn off. X is properly reinitialized when re-enabling.
#!/bin/sh
# Enable and disable HDMI output on the Raspberry Pi
is_off ()
{
tvservice -s | grep "TV is off" >/dev/null
}
case $1 in
off)
tvservice -o
;;
on)
if is_off
then
tvservice -p
curr_vt=`fgconsole`
if [ "$curr_vt" = "1" ]
then
chvt 2
chvt 1
else
chvt 1
chvt "$curr_vt"
fi
fi
;;
status)
if is_off
then
echo off
else
echo on
fi
;;
*)
echo "Usage: $0 on|off|status" >&2
exit 2
;;
esac
exit 0
@arionl

This comment has been minimized.

Show comment
Hide comment
@arionl

arionl Mar 30, 2014

I have a standard console on VT1 and X11 on VT2. When I ran your script initially, it wasn't working. I poked around a bit and it seems that the VT numbers were getting mixed up for some reason. I added some sleeps in between the chvt commands and that seems to have fixed things. I let it burn in for a few hours running:

while true; do sudo ./rpi-hdmi off; sleep 10; sudo ./rpi-hdmi on; sleep 60; done

...and it seems to be working consistently now. Thanks for your help with this solution!

arionl commented Mar 30, 2014

I have a standard console on VT1 and X11 on VT2. When I ran your script initially, it wasn't working. I poked around a bit and it seems that the VT numbers were getting mixed up for some reason. I added some sleeps in between the chvt commands and that seems to have fixed things. I let it burn in for a few hours running:

while true; do sudo ./rpi-hdmi off; sleep 10; sudo ./rpi-hdmi on; sleep 60; done

...and it seems to be working consistently now. Thanks for your help with this solution!

@Chalta

This comment has been minimized.

Show comment
Hide comment
@Chalta

Chalta Feb 24, 2017

Just a note to other users, the fgconsole command must be called with sudo privileges or you will get an error:

Couldn't get a file descriptor referring to the console

Chalta commented Feb 24, 2017

Just a note to other users, the fgconsole command must be called with sudo privileges or you will get an error:

Couldn't get a file descriptor referring to the console

@IBKDan

This comment has been minimized.

Show comment
Hide comment
@IBKDan

IBKDan Mar 30, 2017

@Chalta - are you saying you modified the line from:
curr_vt=`fgconsole`
to be:
curr_vt=sudo `fgconsole`

IBKDan commented Mar 30, 2017

@Chalta - are you saying you modified the line from:
curr_vt=`fgconsole`
to be:
curr_vt=sudo `fgconsole`

@jotom1809

This comment has been minimized.

Show comment
Hide comment
@jotom1809

jotom1809 Apr 5, 2017

Hello
Is there a way to switch between hdmi and composite video without rebooting?

Hello
Is there a way to switch between hdmi and composite video without rebooting?

@quelleck

This comment has been minimized.

Show comment
Hide comment
@quelleck

quelleck Apr 6, 2017

@IBKDan I found some comments suggesting vcgencmd instead of tvservice (info here). I forked this and used vcgencmd instead. So far no issues, and no need for sudo.

quelleck commented Apr 6, 2017

@IBKDan I found some comments suggesting vcgencmd instead of tvservice (info here). I forked this and used vcgencmd instead. So far no issues, and no need for sudo.

@leemcarthur

This comment has been minimized.

Show comment
Hide comment
@leemcarthur

leemcarthur Jul 16, 2017

Hi all. I am trying to use this script to turn my raspberry pi on/off at different times depending if its a work day or a weekend.
I know how to setup the weekday AND weekend requests separate from one another. 

My requirements: 
M-F: (turn it off all day, only back on when we get home after work)
Every day during the week I want the HDMI port on at 7AM, and off at 9AM. I want it back on when we get home at work from 5:00 PM and to turn off for the day at 11PM.

Sat-Sun: (leave it on all day)
On at 7AM, Off at 11PM.

So since I have a global on and off time of 7AM and 11PM every day we could say:
00 7 * * * * /home/pi/rpi-hdmi.sh on
00 23 * * * * /home/pi/rpi-hdmi.sh off

Then for the weekdays:
00 9 * * 1–5 /home/pi/rpi-hdmi.sh off (off at 9AM)
00 17 * * 1–5 /home/pi/rpi-hdmi.sh on (on at 5PM)

My question to you is one of order — how do I know if these will be executed in this order? Is there a better way to write this?
Thanks!

Hi all. I am trying to use this script to turn my raspberry pi on/off at different times depending if its a work day or a weekend.
I know how to setup the weekday AND weekend requests separate from one another. 

My requirements: 
M-F: (turn it off all day, only back on when we get home after work)
Every day during the week I want the HDMI port on at 7AM, and off at 9AM. I want it back on when we get home at work from 5:00 PM and to turn off for the day at 11PM.

Sat-Sun: (leave it on all day)
On at 7AM, Off at 11PM.

So since I have a global on and off time of 7AM and 11PM every day we could say:
00 7 * * * * /home/pi/rpi-hdmi.sh on
00 23 * * * * /home/pi/rpi-hdmi.sh off

Then for the weekdays:
00 9 * * 1–5 /home/pi/rpi-hdmi.sh off (off at 9AM)
00 17 * * 1–5 /home/pi/rpi-hdmi.sh on (on at 5PM)

My question to you is one of order — how do I know if these will be executed in this order? Is there a better way to write this?
Thanks!

@usafle

This comment has been minimized.

Show comment
Hide comment
@usafle

usafle Nov 30, 2017

@leemcarthur: Did you ever figure out a solution to that?

usafle commented Nov 30, 2017

@leemcarthur: Did you ever figure out a solution to that?

@wmcollins

This comment has been minimized.

Show comment
Hide comment
@wmcollins

wmcollins Feb 16, 2018

@leemcarthur: Did you ever figure out a solution?

Here's the solution. I think you will have problem each on/off for each day. See http://ediy.com.my/index.php/tutorials/item/106-raspberry-pi-schedule-turn-off-on-display

wmcollins commented Feb 16, 2018

@leemcarthur: Did you ever figure out a solution?

Here's the solution. I think you will have problem each on/off for each day. See http://ediy.com.my/index.php/tutorials/item/106-raspberry-pi-schedule-turn-off-on-display

@robnson815

This comment has been minimized.

Show comment
Hide comment
@robnson815

robnson815 Feb 22, 2018

@Chalta if you add sudo to all of the commands, that should fix teh Couldn't get a file descriptor referring to the console error,

#!/bin/sh

Enable and disable HDMI output on the Raspberry Pi

is_off ()
{
sudo tvservice -s | grep "TV is off" >/dev/null
}

case $1 in
off)
sudo tvservice -o
;;
on)
if is_off
then
sudo tvservice -p
curr_vt=sudo fgconsole
if [ "$curr_vt" = "1" ]
then
sudo chvt 2
sudo chvt 1
else
sudo chvt 1
sudo chvt "$curr_vt"
fi
fi
;;
status)
if is_off
then
echo off
else
echo on
fi
;;
*)
echo "Usage: $0 on|off|status" >&2
exit 2
;;

@Chalta if you add sudo to all of the commands, that should fix teh Couldn't get a file descriptor referring to the console error,

#!/bin/sh

Enable and disable HDMI output on the Raspberry Pi

is_off ()
{
sudo tvservice -s | grep "TV is off" >/dev/null
}

case $1 in
off)
sudo tvservice -o
;;
on)
if is_off
then
sudo tvservice -p
curr_vt=sudo fgconsole
if [ "$curr_vt" = "1" ]
then
sudo chvt 2
sudo chvt 1
else
sudo chvt 1
sudo chvt "$curr_vt"
fi
fi
;;
status)
if is_off
then
echo off
else
echo on
fi
;;
*)
echo "Usage: $0 on|off|status" >&2
exit 2
;;

@andyh12

This comment has been minimized.

Show comment
Hide comment
@andyh12

andyh12 May 27, 2018

Hi, never done any coding whatsoever before, but thought I’d try my hand with dakboard. I’ve loaded it up and working on my monitor through raspberry pi, and followed the script above but I’m getting lost with how to complete the rest of it. Is there an idiots guide to getting this to work?
Thanks in advance

andyh12 commented May 27, 2018

Hi, never done any coding whatsoever before, but thought I’d try my hand with dakboard. I’ve loaded it up and working on my monitor through raspberry pi, and followed the script above but I’m getting lost with how to complete the rest of it. Is there an idiots guide to getting this to work?
Thanks in advance

@leemcarthur

This comment has been minimized.

Show comment
Hide comment
@leemcarthur

leemcarthur Jun 8, 2018

@usafle & @wmcollins - yes I did figure it out. I wrote out the syntax here after much trial and error:

I have a whole other issue with crontab not running at all, or rather, running two processes right now:

ps -eaf | grep cron

Output is:

root 360 1 0 20:58 ? 00:00:00 /usr/sbin/cron -f
pi 1303 1294 0 20:59 pts/0 00:00:00 grep --color=auto cron

So its running as the pi user as well as root? Could this be the issue why my rpi-hdmi.sh script isn't running?

@usafle & @wmcollins - yes I did figure it out. I wrote out the syntax here after much trial and error:

I have a whole other issue with crontab not running at all, or rather, running two processes right now:

ps -eaf | grep cron

Output is:

root 360 1 0 20:58 ? 00:00:00 /usr/sbin/cron -f
pi 1303 1294 0 20:59 pts/0 00:00:00 grep --color=auto cron

So its running as the pi user as well as root? Could this be the issue why my rpi-hdmi.sh script isn't running?

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