21 Haziran 2013 Cuma

Unix shell scripting - Log yazma fonsiyonu


Log-function

In a private message, I was asked how the log-function worked that I described above. Since this might be helpful to other people too, I'm answering the question in public.

First of all, I use Bash, don't know if this trick works in other Shells.

Content of script:


Code:
#!/bin/bash
. lib/functions.conf # mind the SPACE between the DOT and the path/filename!!! 
logfile=scheduler.log
func_eventlog "STARTING ABG JOB"
-=script runs some more, but that's irrelevant, the example is clear=-

Content of file: lib/functions.conf which by the way is executable :


Code:
################################################
#                                              #
# Log functie                                  #
#                                              #
# Usage:                                       #
#                                              #
# func_eventlog "Dit wil ik loggen"            #
#                                              #
# Variabelen:                                  #
#                                              #
# $logfile (logfile inclusief pad)             #
#                                              #
################################################

func_eventlog()
{
 echo -e "`date` - $1" >> $logfile
}

So whenever I want something logged, I just use:

func_eventlog "Send this text to logfile"

and I never ever have to worry about accidentally overwriting my logs instead of appending to them. 

Hiç yorum yok: