bash脚本挂起 [英] Bash script hangs

查看:351
本文介绍了bash脚本挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下code。基本上这code将 LS -tr $ FROM_DIRECTORY 然后将输出重定向到 /tmp/msc.load.tmp 。在此之后一个循环将执行与第3000文件移动到另一个目录。在code为工作正常,但有时它会挂起。我不知道为什么它挂起。任何人都知道什么是脚本的问题?

  LS -tr $ FROM_DIRECTORY> /tmp/msc.load.tmp
回声$ SYSDATE - 上市Diretory>> $ LOGFILE
#从文件列表中,从顶部获得3000移动。跳过列表中的剩余的文件
#在这个迭代。
#1.1版 - 从临时文件列表中的文件。
在$文件(猫/tmp/msc.load.tmp | grep的。MSCERC *'|头-3000)

   MV $ FROM_DIRECTORY / $文件$ DESTINATION_DIRECTORY
DONE
回声$ SYSDATE - 完脚本的>> $ LOGFILE
退出0
#脚本结束。


解决方案

叶氏,尝试发现。

另外,如果你没有意识到这一点,设置-x命令是在这样的情况有价值。我的方法是设定-x添加到脚本的顶部,然后重定向到一个文件输出运行。无论捕获标准输出和标准误差

./ script.sh> output.txt的2>&安培; 1

如果你愿意,你可以尾部-f在另一个窗口output.txt的监测进展情况。

设置-x回声命令运行并重定向放命令和输出到cronological秩序。

I have the code below. Basically this code will ls -tr $FROM_DIRECTORY then redirect the output to /tmp/msc.load.tmp. After this a for loop 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
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.

解决方案

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

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

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

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

这篇关于bash脚本挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆