How To Create A Log File In Shell Script
fred974
Daemon
Reaction score: 47
Messages: 1,628
- #1
Hello all,
I have done a post install script that install various packages.. For example I have:
Code:
echo "# Updating The Ports Collection" portsnap fetch extract rehash echo "# Enable PKGNG as new package format" echo 'WITH_PKGNG=yes' >> /etc/make.conf echo "# convert your /var/db/pkg database to the new pkg format" pkg2ng echo "# Install portmaster management tool." make -C /usr/ports/ports-mgmt/portmaster BATCH=yes OPTIONS_FILE_SET="BASH ZSH" install clean echo "# Check for outdated port" portmaster -L What I want to know is how can have create a log file to see id there was any error/warning messages that occurred whilst the script was ran?
Could anyone help.
I'll read any documentation you have to offer.
Thank you
SirDice
Administrator
Reaction score: 12,754
Messages: 39,353
- #2
You don't need to run
rehashwith a shell script. That's only needed for interactive csh(1), not for sh(1).
The simplest way is to redirect all the output of the script to a file:
./myscript.sh > mylogfile.txt
You can also add redirection to the script itself.
fred974
Daemon
Reaction score: 47
Messages: 1,628
- Thread Starter
- #3
Cool thank you @SirDice this will indeed do it.
Since posted the message I tried to do the following:
Code:
touch ${HOME}/postinstall/logs/logouput touch ${HOME}/postinstall/logs/logerror logouput=??? logerror=??? LOGPATH=${HOME}/postinstall/logs ERRORLOG=${LOGPATH}/${logerror}_`date +%d/%m/%y_%H:%M:%S`.log OUTPUTLOG=${LOGPATH}/${logouput}_`date +%d/%m/%y_%H:%M:%S`.log exec 1> ${OUTPUTLOG} exec 2> ${ERRORLOG} echo `date +%d/%m/%y_%H:%M:%S` : ----------- START of ${OUTPUTLOG} --------------- 1> echo `date +%d/%m/%y_%H:%M:%S` : ----------- START of ${ERRORLOG} --------------- 2> Is there any chance you could help me with what to replace the ?? with.
Thank you
Last edited by a moderator:
SirDice
Administrator
Reaction score: 12,754
Messages: 39,353
- #4
Looks like it could be anything, the script simply adds the date to the end. So it's just a common name you want to use for the log file itself.
tzoi516
Well-Known Member
Reaction score: 12
Messages: 368
- #5
What about using script ?
Code:
# script /tmp/output.txt # ./myscript.sh # exit # less /tmp/output.txt
worldi
Active Member
Reaction score: 23
Messages: 189
- #6
+1 for script().
Note that you can also specify the command you want to run. This allows for nice aliases:
Code:
alias svnup_ports="script -aq /var/log/svnup.ports svn up /usr/ports" alias svnup_src="script -aq /var/log/svnup.src svn up /usr/src"
fred974
Daemon
Reaction score: 47
Messages: 1,628
- Thread Starter
- #7
tzoi516 said:
What about using
script?Code:
# script /tmp/output.txt # ./myscript.sh # exit # less /tmp/output.txt
Sorry I am not aware of 'script' could you please give more info?
I tried to change my original attemtp an got that:
Code:
mkdir -p ${HOME}/scripts/scriplogs/postinstall/ LOGPATH=${HOME}/scripts/scriplogs/postinstall/ OUTPUTLOG=${LOGPATH}/logouput_`date +%d/%m/%y_%H:%M:%S`.log ERRORLOG=${LOGPATH}/logerror_`date +%d/%m/%y_%H:%M:%S`.log # Redirect all stdout and stderr to respective files exec 1> ${OUTPUTLOG} exec 2> ${ERRORLOG} Could anyone critic my last attempt?
Thank you
- #8
fred974 said:
Sorry I am not aware of 'script' could you please give more info?
Type man script.
Tip: You can do this for (almost) all commands, and is probably one of the most useful sources of help on your FreeBSD system.
Type man man for general information about manpages.
fred974
Daemon
Reaction score: 47
Messages: 1,628
- Thread Starter
- #9
ok... I created a simple shell script
Code:
#!/bin/sh # mkdir -p ${HOME}/scripts/logs/postinstall/ LOGPATH=${HOME}/scripts/logs/postinstall OUTPUTLOG=${LOGPATH}/logouput_`date +%d/%m/%y_%H:%M:%S`.log ERRORLOG=${LOGPATH}/logerror_`date +%d/%m/%y_%H:%M:%S`.log # Redirect all stdout and stderr to respective files exec 1> ${OUTPUTLOG} exec 2> ${ERRORLOG} echo "# Synchronize The Local Clock." make -C /usr/ports1/net/openntpd/ BATCH=yes install I had the following outcome
Code:
root@FreeBSD:~/scripts # ./test123.sh ./test123.sh: cannot create /root/scripts/logs/postinstall/logouput_29/01/14_21:56:41.log: No such file or directory Not sure how to solved that ...
on my way to read man script
- #10
The directory doesn't exist. Use mkdir -p to create it. For example: mkdir -p "$(dirname "$OUTPUTLOG")"
How To Create A Log File In Shell Script
Source: https://forums.freebsd.org/threads/help-creating-a-log-file-for-shell-script.44598/
Posted by: mcdanielcalluser.blogspot.com

0 Response to "How To Create A Log File In Shell Script"
Post a Comment