Don't stop script when connection unavailable
There is a maximum time of waiting for the connection to be available. When reached, the script simply stops, closing the data file. Installed as a service, it will restart and create a new file. Maybe it is not bad idea just keep trying and not create a new file.