1、问题:
最近需要在远程跑Matlab代码,找来了一段后台运行命令:
nohup matlab -nodisplay -nosplash -r matlab_command > outfile.txt &
运行命令 matlab_command 后,将结果输出到 outfile.txt 中。但后来发现,当程序运行因错误中止时,之后的输出结果总会循环输出Bad file descriptor和 Warning: "Error reading character from command line" 这两条信息,使输出文件很大,占用空间,被管理员各种邮件警告了很多次,很是烦恼。
Warning: "Error reading character from command line"
Warning: "Error reading character from command line"
Warning: "Error reading character from command line"
Warning: "Error reading character from command line"
Warning: "Error reading character from command line"
Warning: "Error reading character from command line"
Warning: "Error readinBad file descriptor
Bad file descriptor
Bad file descriptor
Bad file descriptor
Bad file descriptor
Bad file descriptor
Bad file descriptor
2、解决方案:
(1)方案1:我首先找到了这篇博客
https://blog.csdn.net/sethinking/article/details/88375995
将matlab执行语句,改为执行相应文件的命令形式,即:
nohup matlab -nodisplay -nosplash <matlab_file.m> > outfile.txt &
但我相应的matlab文件中有很多内置的 function,这样去执行matlab代码会导致 function 识别不出来,运行不了,我还是希望能够通过执行命令行的方式,来运行文件。
(2)方案2:在 & 前加上 </dev/null
https://realwecan.blogspot.com/2013/10/infinite-error-loop-when-running-matlab.html
nohup matlab -nodisplay -r matlab_command > outfile.txt </dev/null &
使用了这个方案后,问题迎刃而解。但为什么可以成功,原博中没有细说,只是说引起这个问题的是字符 &。在这个字符前加上就可以解决这个问题了。如有懂得其中原理的朋友,希望能够不吝赐教。