简体   繁体   English

在bash中创建详细的自我跟踪日志

[英]Create a detailed self tracing log in bash

I know you can create a log of the output by typing in script nameOfLog.txt and exit in terminal before and after running the script, but I want to write it in the actual script so it creates a log automatically. 我知道您可以通过输入script nameOfLog.txt来创建输出日志,并在运行脚本之前和之后在终端中exit ,但是我想在实际脚本中编写它,以便它自动创建日志。 There is a problem I'm having with the exec >>log_file 2>&1 line: 我的exec >>log_file 2>&1行有一个问题:

The code redirects the output to a log file and a user can no longer interact with it. 该代码将输出重定向到日志文件,并且用户无法再与其进行交互。 How can I create a log where it just basically copies what is in the output? 如何创建一个日志,该日志基本上只复制输出中的内容?

And, is it possible to have it also automatically record the process of files that were copied? 而且,是否还可以自动记录复制文件的过程? For example, if a file at /home/user/Deskop/file.sh was copied to /home/bckup, is it possible to have that printed in the log too or will I have to write that manually? 例如,如果将/home/user/Deskop/file.sh中的文件复制到/ home / bckup,是否也可以在日志中打印该文件,还是必须手动编写?

Is it also possible to record the amount of time it took to run the whole process and count the number of files and directories that were processed or am I going to have to write that manually too? 是否还可以记录运行整个过程所花费的时间并计算已处理的文件和目录的数量,还是我也必须手动编写?

My future self appreciates all the help! 我未来的自我感谢所有的帮助!

Here is my whole code: 这是我的整个代码:

#!/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

To answer the "how to just copy the output" question: use a program called tee and then a bit of exec magic explained here: redirect COPY of stdout to log file from within bash script itself 要回答“如何仅复制输出”问题:使用一个名为tee的程序,然后在此处解释一些exec魔术: 从bash脚本本身将stdout的COPY重定向到日志文件

Regarding the analytics (time needed, files accessed, etc) -- this is a bit harder. 关于分析(所需的时间,访问的文件等)-这有点困难。 Some programs that can help you are time(1): 一些可以帮助您的程序是time(1):

time - run programs and summarize system resource usage

and strace(1): 和strace(1):

   strace - trace system calls and signals

Check the man pages for more info. 查看手册页以获取更多信息。 If you have control over the script it will be probably easier to do the logging yourself instead of parsing strace output. 如果您可以控制脚本,则自己记录日志而不是解析strace输出可能会更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM