logging - Create a detailed self tracing log in bash -
i know can create log of output typing in script nameoflog.txt
, exit
in terminal before , after running script, want write in actual script creates log automatically. there problem i'm having exec >>log_file 2>&1
line:
the code redirects output log file , user can no longer interact it. how can create log copies in output?
and, possible have automatically record process of files copied? example, if file @ /home/user/deskop/file.sh copied /home/bckup, possible have printed in log or have write manually?
is possible record amount of time took run whole process , count number of files , directories processed or going have write manually too?
my future self appreciates help!
here whole code:
#!/bin/bash collect() { find "$directory" -name "*.sh" -print0 | xargs -0 cp -t ~/bckup #xargs handles files names spaces. gives error of "cp: not overwrite just-created" if file didn't exist } echo "starting log" exec >>log_file 2>&1 timelimit=10 echo "please enter directory collect. if no input in 10 secs, default of /home selected" read -t $timelimit directory if [ ! -z "$directory" ] #if directory doesn't have length of 0 echo -e "\nyou want copy $directory." #-e \n work , won't show part of string else directory=/home/ echo "time's up. backup in $directory" fi if [ ! -d ~/bckup ] echo "directory not exist, creating now" mkdir ~/bckup fi collect echo "finished collecting" exit 0
to answer "how copy output" question: use program called tee , bit of exec magic explained here: redirect copy of stdout log file within bash script itself
regarding analytics (time needed, files accessed, etc) -- bit harder. programs can time(1):
time - run programs , summarize system resource usage
and strace(1):
strace - trace system calls , signals
check man pages more info. if have control on script easier logging instead of parsing strace output.
Comments
Post a Comment