Based on a project from the Homeassistant commuity
sudo apt-get install libcec-dev build-essential python3-dev python3-pip git
git clone https://github.com/Josua019/python-cec.git
sudo python3 setup.py install
python3 cecwatcher.py
A log file will be created and the console will output relevant information
Stop the script with Ctrl+C
sudo nano /etc/systemd/system/cecwatcher.service
update the path at 'ExecStart'
# systemd unit file for cecwatcher.py
[Unit]
Description=CEC Watcher
[Service]
ExecStart=/usr/bin/python3 /home/pi/Documents/python-cec/cecwatcher.py
# Disable Python's buffering of STDOUT and STDERR, so that output from the
# service shows up immediately in systemd's logs
Environment=PYTHONUNBUFFERED=1
User=pi
[Install]
# Tell systemd to automatically start this service when the system boots
# (assuming the service is enabled)
WantedBy=default.target
sudo systemctl start cecwatcher
sudo systemctl status cecwatcher
sudo systemctl enable cecwatcher
If status shows an error try fixing it, run
sudo systemctl reset-failed
and resart the service