Only start kodi if HDMI is detected

Else start headless…

How can I do this?

Have a look at this. Should get you started:

I made this script but I don’t know how to make it into the boot sequence. I tried disabling the mediacenter service but it always starts up on boot and I don’t want to break anything.

rm -f /home/osmc/scripts/tvstatus.log
tvservice -dumpedid /home/osmc/scripts/tvstatus.log
rm -f /home/osmc/scripts/hdmikodi.log
if [ -e /home/osmc/scripts/tvstatus.log ]; then
    echo "HDMI"
    echo "HDMI" > /home/osmc/scripts/hdmikodi.log
    /usr/lib/kodi/kodi.bin --standalone -fs --lircdev /var/run/lirc/lircd &
    rm /home/osmc/scripts/tvstatus.log
    echo "HEADLESS"
    echo "HEADLESS" > /home/osmc/scripts/hdmikodi.log

The “obvious” thing to do is modify /usr/bin/mediacenter to check if tvservice indicates the presence of HDMI but any such changes will be lost each time a new version of the script is installed.

A better method is to modify the systemd unit file /lib/systemd/system/mediacenter.service with a “drop in” file that will survive an update to the base unit file.

In this case, you’d need to create a new directory with the command:

sudo mkdir /lib/systemd/system/mediacenter.service.d

and then in the new directory, create a file called tvservice.conf. (The first part of the name isn’t important, but it must end with .conf). Edit the file, using sudo, and add just two lines:

ExecStartPre = /usr/local/bin/ (or whatever you choose to call it is a shell script that I’ll show below. In the post linked to by @nrosier, there’s a list of possible responses from running the tvservice command. For this example, I used the response from running tvservice -n, which means print the device ID from EDID. It might need some refinement since my Pi isn’t currently connected to a TV, so all I ever get is one response: “[E] No device present”. If it gets this “[E] No device present” returned, it exits with a return code of 99, otherwise it exits with a return code of zero. The important thing to note is that if the ExecStartPre doesn’t return a zero return code, the whole mediacenter unit will fail. If I run systemctl status mediacenter, I now get:

osmc@osmc:~$ sudo systemctl status mediacenter
● mediacenter.service - media center application
   Loaded: loaded (/lib/systemd/system/mediacenter.service; enabled)
  Drop-In: /lib/systemd/system/mediacenter.service.d
   Active: failed (Result: exit-code) since Fri 2017-11-24 19:56:26 GMT; 1h 5min ago
  Process: 27971 ExecStartPre=/usr/local/bin/ (code=exited, status=99)
 Main PID: 27768 (code=exited, status=0/SUCCESS)

Nov 24 19:56:26 osmc systemd[1]: mediacenter.service: control process exited, code=exited status=99
Nov 24 19:56:26 osmc systemd[1]: Failed to start media center application.
Nov 24 19:56:26 osmc systemd[1]: Unit mediacenter.service entered failed state.

The shell script, might need a bit more work, but this is my “proof of concept” code:

osmc@osmc:~$ cat /usr/local/bin/ 
tvs=`/opt/vc/bin/tvservice -n 2>&1 >/dev/null`
if [ "$tvs" = "[E] No device present" ]; then exit 99; else exit 0; fi

This is gold!!! Thanks!! Maybe add this to your faq!