2013-05-13 88 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輸出。