help creating a log file for shell script

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
 
You don't need to run rehash with 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.
 
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:
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.
 
+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"
 
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
 
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.
 
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
 
Back
Top