Use exec to redirect output to log instead of subshell

Avoid forking a subshell by using exec to redirect output to log file.
Additional logging improvements are: Set filename at the top of script once
instead of hard-coding it throughout the script.  Log both stdout and stderr.
This commit is contained in:
Peter Ajamian 2021-05-07 16:22:45 +12:00
parent ce79f250ce
commit 029f172fe3

View file

@ -4,8 +4,12 @@
## Rocky is RC status. Using this script means you accept all risks of system instability.
(
# Pass everything to a subshell so the output can be piped to /var/log/centos2rocky.log
# Path to logfile
logfile=/var/log/centos2rocky.log
# Send all output to the logfile as well as stdout.
truncate -s0 "$logfile"
exec > >(tee -a "$logfile") 2> >(tee -a "$logfile" >&2)
errcolor=$(tput setaf 1)
nocolor=$(tput op)
@ -81,7 +85,7 @@ final_message() {
}
logmessage(){
printf '%s\n' "${blue}A log of this installation can be found at /var/log/centos2rocky.log$nocolor"
printf '%s\n' "${blue}A log of this installation can be found at $logfile$nocolor"
}
## The actual work
@ -286,6 +290,3 @@ printf '\n\n\n'
cat /etc/issue | awk 'NR<=15'
printf '%s\n' "$blue" "Done, please reboot your system.$nocolor"
logmessage
) | tee /var/log/centos2rocky.log
# Pipe output to /var/log/centos2rocky.log