I need some help please

I have a complex programme running through a MIAC which controls an inverter via the outputs, so i have specific commands eg output 1 = on for 30 s and then output 2 on for 10s
The problem i have is controlling the relay, i want to have the relay on but once the input is activated turn the relay off and as soon as the input drops then the relay to come back on instantly without having to wait for the loop top start again.
Can any one help please??
Thanks
