You do not need to tell the Arduino that it is working on external power.
How are you connecting the 9V battery to the Arduino ?
If it is a PP3 battery then its life will be extremely limited. Have you measured the voltage of the battery when it is powering the Arduino ?
How much current does the relay take ? Can you provide a link to it ?
Try this series of actions.
Connect via USB and upload the program. How long is it before the relay turns on ?
Disconnect USB and connect the 9V battery (how are you connecting it ?). What is the battery voltage ? How long before the relay turns on, if at all ?
Disconnect the 9V battery and reconnect the USB. Do NOT upload the program again. How long is it before the relay turns on ?
Apart from the above, have you taken on board that once turned on the relay will never turn off when using the program that you posted ?