I have a latexmkrc file that looks like this:
$xelatex = 'xelatex -interaction=nonstopmode -synctex=1 %O %S; (sleep 10) && (test -f "%Z%R.synctex.gz") && (cp "%Z%R.synctex.gz" "%R.synctex.gz"); (test -f "%D") && (cp "%D" "%R.pdf")';
$pdflatex = 'pdflatex -interaction=nonstopmode -synctex=1 %O %S; (test -f "%Z%R.synctex.gz") && (cp "%Z%R.synctex.gz" "%R.synctex.gz"); (test -f "%D") && (cp "%D" "%R.pdf")';
$out_dir = '/tmp';
Latexmk automatically adds -no-pdf since the %O is specified. If this is the case, then it seems the commands after the xelatex command are run before the conversion of the extended dvi to pdf using xdvipdfmx. My .pdf file is always corrupted in the local folder but if I open it in the /tmp folder it is fine. Is there a way to run these copy commands after the command for xdvipdfmx when using the -no-pdf.
Here is an image of what happens at the end. This is run after the copy commands above are already run. The pdf file copied is always corrupted.

As stated in the question, the problem is that the definitions given in the question assume that
xelatexis used bylatexmkto make a pdf file. In fact,latexmkarranges forxelatexto make an xdv file, and then callsxdvpdfmxto make the pdf file from the xdv file. (The reason thatlatexmkdoes this is that for a graphics intensive documents, this can result in substantial shortening of run time if multiple runs ofxelatexare needed.)A suitable solution is to redefine also the command for the xdv-to-pdf conversion as follows
(Note some of the lines in the code sample are long, so scroll the box to see all of the codes.)