write to file periodically