LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 04-08-2004, 06:38 PM   #1
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Rep: Reputation: 30
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?
 
Old 04-08-2004, 07:10 PM   #2
blish_blash
Member
 
Registered: Dec 2003
Posts: 68

Rep: Reputation: 15
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
 
Old 04-09-2004, 05:52 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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.
 
Old 04-11-2004, 02:29 AM   #4
meonkeys
Member
 
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
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
 
Old 07-21-2004, 10:37 PM   #5
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by Hko
I would use "wc -l" for that.
Is there a way to get wc to not repeat the filename?
 
Old 07-21-2004, 11:00 PM   #6
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
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
 
Old 07-22-2004, 06:23 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
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"
 
Old 07-22-2004, 09:24 AM   #8
kleptophobiac
Member
 
Registered: Jun 2003
Distribution: Arch
Posts: 315

Original Poster
Rep: Reputation: 30
That seems kind of backward, but ooookay. Still somewhat better than the grep solution.
 
Old 07-22-2004, 10:21 AM   #9
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally posted by kleptophobiac
That seems kind of backward, but ooookay.
Just for completeness....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
script to allow for logging-in dabash Linux - Newbie 3 12-29-2006 04:15 PM
logging a shell/bash session? JustinHoMi Linux - Software 6 04-10-2005 12:34 AM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
bash script prob: how can i tell the script that a 'dd' has finished? Frustin Linux - General 2 04-02-2003 06:34 AM
FYI: Bash-2.05.51+logging unSpawn Linux - Security 0 02-06-2003 07:14 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration