2013-05-13 91 views
0

我知道你可以在运行脚本前后在终端中输入script nameOfLog.txtexit来创建输出日志,但是我想将它写入实际脚本中,以便创建一个自动登录。我遇到了exec >>log_file 2>&1行:在bash中创建详细的自我跟踪日志

代码将输出重定向到日志文件,用户不能再与其交互。我怎样才能创建一个日志,它只是基本上复制输出中的内容?

而且,是否有可能让它自动记录被复制的文件的进程?例如,如果/home/user/Deskop/file.sh中的文件被复制到/ home/bckup,是否也可以在日志中打印该文件,或者是否必须手动编写该文件?

是否有可能记录运行整个过程所需的时间,并计算已处理的文件和目录的数量,还是我将不得不手动编写该文件和目录?

我未来的自我感谢所有的帮助!

这里是我的全部代码:

#!/bin/bash  

collect() 
{ 
find "$directory" -name "*.sh" -print0 | xargs -0 cp -t ~/bckup #xargs handles files names with spaces. Also gives error of "cp: will not overwrite just-created" even if file didn't exist previously 
} 
echo "Starting log" 
exec >>log_file 2>&1 
timelimit=10 
echo "Please enter the directory that you would like to collect. 
If no input in 10 secs, default of /home will be selected" 

read -t $timelimit directory 

if [ ! -z "$directory" ] #if directory doesn't have a length of 0 
then 
echo -e "\nYou want to copy $directory." #-e is so the \n will work and it won't show up as part of the string 
else 
directory=/home/ 
echo "Time's up. Backup will be in $directory" 
fi 

if [ ! -d ~/bckup ] 
then 
echo "Directory does not exist, creating now" 
mkdir ~/bckup 
fi 

collect 
echo "Finished collecting" 

exit 0 

回答

1

要回答“怎么刚才复制的输出”的问题:使用的程序调用发球,然后一点点EXEC魔这里解释: redirect COPY of stdout to log file from within bash script itself

关于分析(所需时间,文件访问等) - 这有点困难。某些程序,可以帮助你在时间(1):

time - run programs and summarize system resource usage 

和strace的(1):

strace - trace system calls and signals 

检查更多信息的手册页。如果您可以控制脚本,那么您可能更容易自己执行日志记录,而不是解析strace输出。