How to shutdown safely when freeze?

How can I shutdown the server without damaging the minecraft map in the event of a freeze?

And below is the error.

You should honestly be fine if you just kill the server. The worst that happens for me, at least, is that the map rollbacks a few minutes. Of course there is still risk of world corruption but it’s quite unlikely. Just take regular backups to be sure and you should be fine.