you can get the program state via Real-time Interface (30003)
jbm provided an example (prgstate): URCap Sample: getRobotData Real Time Client reader URCap
https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/remote-control-via-tcpip-16496/
alternative via RTDE output: runtime_state
https://www.universal-robots.com/how-tos-and-faqs/how-to/ur-how-tos/real-time-data-exchange-rtde-guide-22229/