If you are looking to run the script as a daemon, one good approach would be to use the Python Daemon library. The code below should do what you are looking to achieve:
import daemon
import time
def write_time_to_file():
with open("file.txt", "a") as f:
hora = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime())
f.write(hora+"\n")
with daemon.DaemonContext():
while(True):
write_time_to_file()
time.sleep(2)
Tested this locally and it worked fine, appending time to the file every 2 seconds.