Bash 脚本挂起

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6971257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:32:03  来源:igfitidea点击:

Bash script hangs

bashunixscripting

提问by user871695

I have the code below. Basically this code will ls -tr $FROM_DIRECTORYthen redirect the output to /tmp/msc.load.tmp. After this a forloop will execute and move the first 3000 files to another directory. The code is working fine but sometimes it will hang. I have no idea why it hangs. Anyone know what is the problem of the script?

我有下面的代码。基本上这段代码会将ls -tr $FROM_DIRECTORY输出重定向到/tmp/msc.load.tmp. 在此之后,for将执行一个循环并将前 3000 个文件移动到另一个目录。代码运行良好,但有时会挂起。我不知道为什么它挂起。有谁知道脚本的问题是什么?

ls -tr $FROM_DIRECTORY > /tmp/msc.load.tmp
echo "$sysdate -- Listing Diretory " >>$LOGFILE
# From the file list, get the 3000 from the top to move. Skip the remaining files in the list 
# in this iteration. 
# Version 1.1 - List the files from the temporary file.  
for file in $(cat /tmp/msc.load.tmp | grep 'MSCERC.*' | head -3000 )
do 
   mv $FROM_DIRECTORY/$file $DESTINATION_DIRECTORY
done
echo "$sysdate -- End of Script " >>$LOGFILE
exit 0
# End of script.

回答by Karl

Yeap, try a find.

是的,尝试查找。

Also if you're not aware of it, the set -x command is valuable in situations like this. My approach is to add set -x to the top of the script and then run it with output redirected to a file. capturing both standard out and standard error

此外,如果您不知道, set -x 命令在这种情况下很有价值。我的方法是将 set -x 添加到脚本的顶部,然后在输出重定向到文件的情况下运行它。捕获标准输出和标准错误

./script.sh > output.txt 2>&1

./script.sh > output.txt 2>&1

If you want you can tail -f output.txt in another window to monitor progress.

如果需要,您可以在另一个窗口中使用 tail -f output.txt 来监控进度。

Set -x echos the commands run and the redirection puts the command and output into cronological order.

Set -x 回显运行的命令,重定向将命令和输出按时间顺序排列。