因执行nohup而产生的Bad file descriptor 和 Warning: "Error reading character from command line" 错误解决

时间:2025-04-21 08:11:05

nohup运行matlab 命令产生了Bad file descriptor 和 Warning: “Error reading character from command line” 的错误
错误原因是nohup下执行matlab的文件的命令格式错误;
原输入为:

nohup matlab -nodesktop -nosplash -nodisplay -r  main > 2>&1   &

以上命令的意思是,服务器后台运行文件,对于一般情况下,执行没有问题,当文件中涉及一些文件的读取及保存操作时会输出Bad file descriptor 和 Warning: “Error reading character from command line” 的错误。

把命令改为:

nohup matlab -nodesktop -nosplash -nodisplay <  > 2>&1   &

问题得到解决。
解释一下:“>” 指把输出的内容重定向到中,2>&1 的意思是把错误信息也输入到中,2指的是标准输入输出错误(stderr),1指的是标准输出(stdout),2>&1意思是2等同于1输出,最后的&是后台运行的意思,与nohup命令合在一起使用。