LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Logging via bash script (http://www.linuxquestions.org/questions/programming-9/logging-via-bash-script-167895/)

kleptophobiac 04-08-2004 05:38 PM

Logging via bash script
 
I've written a couple rather simple bash scripts to automate the updating of audio files. Anyway, I echo some output and append to a file.

My problem is that I make an entry every 5 minutes, and the files gets mighty huge after a while. How can I limit the file to three days, or X KB?

blish_blash 04-08-2004 06:10 PM

with "cat log.file | sed 1d > log.file.temp" you create a duplicate of your log without the first line.

You could use it when the file is to big to remove the first lines.

There could be more elegant solution other than calling "sed 1d" 10 times to remove the first 10 lines - look at the sed docu...

checking if the file is too big should not be such a problem... you could also use "cat log.file | grep ^ -c" to return the number of lines in your file...

Greetings,
BlishBlash

Hko 04-09-2004 04:52 AM

Quote:

Originally posted by blish_blash
checking if the file is too big should not be such a problem... you could also use "cat log.file | grep ^ -c" to return the number of lines in your file...
I would use "wc -l" for that.

meonkeys 04-11-2004 01:29 AM

Quote:

My problem is that I make an entry every 5 minutes, and the files gets mighty huge after a while. How can I limit the file to three days, or X KB?
  • delete the file every so often, by hand or by a crontab entry. It will be created anew next time you open it for writing/appending (with >> shell redirection, for instance)
  • use 'logger' to log to syslog instead of shell redirection... the system logfile may already be automatically rotated (/var/log/messages is on my system)
  • do some tests in the shell (see 'man bash' or google for 'advanced bash scripting guide', for instance) or any programming language to see how big the file is, then delete/move/archive as necessary

kleptophobiac 07-21-2004 09:37 PM

Quote:

Originally posted by Hko
I would use "wc -l" for that.
Is there a way to get wc to not repeat the filename?

kleptophobiac 07-21-2004 10:00 PM

Here's my reverse SSH script thus far:

Code:

#!/bin/bash
### Checks to see if SSH connection is up

### Configurable variables
HOST=
RPORT=
LPORT=
USER=
LOGLOCATION=


### Recursive function to clean up the log file
cleanup ()
{
        if [ `cat $LOGLOCATION | grep ^ -c` -ge 500 ]
        then
                cat $LOGLOCATION | sed 1d > $LOGLOCATION
                cleanup
        fi
}

### If there are no instances, start one
if [ `ps ax | grep -c $RPORT` -lt 1 ]
then
        echo "`date +"%F %T"` - SSH connection not found..." >> $LOGLOCATION
        ssh -fNC -l $USER -i /home/$USER/.ssh/id_rsa -R $RPORT:localhost:$LPORT $HOST
        echo "`date +"%F %T"` - SSH connection reestablished!" >> $LOGLOCATION
else
        echo "`date +"%F %T"` - SSH connection found. No action taken." >> $LOGLOCATION
fi

### Run the log cleaner
cleanup


Hko 07-22-2004 05:23 AM

Quote:

Originally posted by kleptophobiac
Is there a way to get wc to not repeat the filename?
Yes. Instead of using "wc -l filename.log" use "cat filename.log | wc -l"

kleptophobiac 07-22-2004 08:24 AM

That seems kind of backward, but ooookay. Still somewhat better than the grep solution. :)

Hko 07-22-2004 09:21 AM

Quote:

Originally posted by kleptophobiac
That seems kind of backward, but ooookay.
Just for completeness.... :)


All times are GMT -5. The time now is 08:07 PM.