In case this could be useful to others, this is how I solved the issue on my side. In the "cmd" function, plug any command you want to monitor.
#!/bin/bashfunction cmd { sudo lsof -i -n -P | grep 11.12.13.14:8883 }echo "" >previous.logwhile true; do cmd >out.log diff out.log previous.log >/dev/null cc="$?" if [[ "$cc" != "0" ]]; then date=`date --rfc-3339=seconds` epoch=`date +%s` echo "" echo "Changes on $date ($epoch)" cat out.log cp out.log previous.log fi sleep 0.5done