Basically I want to save coordinates and momentum of atoms after n time step so that I can restart my calculation in case of an error or exceeding wall time. My MD code is in Fortran.
As your code is in Fortran, the best thing to do is set a counter to write this file in regular intervals. You can open the restart file inside a if, with stat='replace' to overwrite the previous file. After writing in the file, remember to close it.