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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
04-08-2004, 05:38 PM
|
#1
|
|
Member
Registered: Jun 2003
Distribution: Arch
Posts: 315
Rep:
|
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?
|
|
|
|
04-08-2004, 06:10 PM
|
#2
|
|
Member
Registered: Dec 2003
Posts: 68
Rep:
|
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
|
|
|
|
04-09-2004, 04:52 AM
|
#3
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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.
|
|
|
|
04-11-2004, 01:29 AM
|
#4
|
|
Member
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45
Rep:
|
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
|
|
|
|
07-21-2004, 09:37 PM
|
#5
|
|
Member
Registered: Jun 2003
Distribution: Arch
Posts: 315
Original Poster
Rep:
|
Quote:
Originally posted by Hko
I would use "wc -l" for that.
|
Is there a way to get wc to not repeat the filename?
|
|
|
|
07-21-2004, 10:00 PM
|
#6
|
|
Member
Registered: Jun 2003
Distribution: Arch
Posts: 315
Original Poster
Rep:
|
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
|
|
|
|
07-22-2004, 05:23 AM
|
#7
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
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"
|
|
|
|
07-22-2004, 08:24 AM
|
#8
|
|
Member
Registered: Jun 2003
Distribution: Arch
Posts: 315
Original Poster
Rep:
|
That seems kind of backward, but ooookay. Still somewhat better than the grep solution. 
|
|
|
|
07-22-2004, 09:21 AM
|
#9
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Quote:
Originally posted by kleptophobiac
That seems kind of backward, but ooookay.
|
Just for completeness.... 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 12:10 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|