Capping output redirection log file length


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Capping output redirection log file length
# 1  
Old 10-12-2009
Capping output redirection log file length

I am trying to write a script which will output notifications to a logfile, but I would like to cap the logfile to, let's say, 200 lines.

Specifically I am using custom firmware, DD-wrt, on my router and I am implementing a script to connect to my work vpn. I have a loop that pings a computer behind the vpn, and then sleeps for a minute before doing it again. If the ping fails, I have it write a brief message to a logfile. There is no real storage on the router, so everything is held in memory. If my log file gets too large, I will crash my router. Is there a way to make my logfile remove the top line when appending to the bottom, if the file is more than 200 lines. Currently I just append with '>>', but there must be a smarter way.

The relevant parts of the script are below.

Code:
#!/bin/sh

pingtest () {
 ping -q -c1 $1 >> /dev/null
 echo "$?"
}

vpn_keepalive_host1="192.168.###.###"

while [ 1 ]; do
  if [ "`pingtest $vpn_keepalive_host1`" == "0" ]; then
    sleep 60
  else
    echo `date` "$vpn_keepalive_host1 unreachable" >> /tmp/etc/vpnc/Status.$vpn_keepalive_host1

    #reconnect to vpn
    vpnc-disconnect
    vpnc /tmp/etc/vpnc/vpn.conf --dpd-idle 0
  fi
done

# 2  
Old 10-12-2009
You could try:

Code:
logfile=/tmp/etc/vpnc/Status.$vpn_keepalive_host1
echo `date` "$vpn_keepalive_host1 unreachable" |cat $logfile - |tail -200> $logfile

or e.g.
Code:
log200 () {
  echo "$1"|cat "$2" -|tail -200>"$2"
}

log200 "`date` $vpn_keepalive_host1 unreachable"  /tmp/etc/vpnc/Status.$vpn_keepalive_host1

# 3  
Old 10-13-2009
Thanks,

I'm not sure if it's a bug in the router OS or if this is true all of the time, but apparently sometimes this method works and sometimes it overwrites the original file with only the last entry. A (less than elegant) method I've found that will work is:

Code:
log_clip () {
 echo "$1" | cat "$2" - | tail -n 100 > $pwd/temp
 mv $pwd/temp "$2"
}

However I feel there must be a more elegant way to avoid temporary files using awk or sed, but I'm just not that familiar with them. Can someone help appease the OCD perfectionist in me?
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Command output redirection to file issues

Hi, I have a peculiar issue w.r.t redirecting the command output to a file when using loop. I am redirecting command output to same file in a series of if condition statements, but if one block of if condition statement writes the log to the file , the subsequent block of if condition... (7 Replies)
Discussion started by: ananan
7 Replies

2. Shell Programming and Scripting

Output redirection of c binary file to a file in shell script is failing

I am struck up with a problem and that is with output redirection. I used all the ways for the redirection of the output of c binary to a file, still it is failing. Here are the different ways which I have used: ./a.out | tee -a /root/tmp.txt 2>&1 ./a.out | tee -a /root/tmp.txt 1>&1 ./a.out |... (2 Replies)
Discussion started by: Maya29988
2 Replies

3. Shell Programming and Scripting

Redirection of ls -l output

Hi I am making a script where i want to redirect the output of ls -l to a file Example #ls -l fil1.txt > /opt/temp/a.txt ac: No such file or directory I want to capture output of this command like here output is ac: No such file or directory can anyone help (4 Replies)
Discussion started by: anish19
4 Replies

4. Shell Programming and Scripting

output redirection to existing file question

So I have a existing file that I used the uniq command on and I need to save the output to the same file without changing the file name. I have tried $ uniq filename > filename then when I cat the file it then becomes blank like there is nothing inside. any help would be much appreciated... (0 Replies)
Discussion started by: drew211
0 Replies

5. Shell Programming and Scripting

awk output redirection to file

I have a system stat command running which generates data after 5 sec or so. I pass this data to awk and do some calculation to present the data differently. Once done now I want to pass this data to file as and when generated but doesn't work..unless the first command completes successfully.... (6 Replies)
Discussion started by: learnscript
6 Replies

6. UNIX for Dummies Questions & Answers

Output file redirection

Suppose I have a file named a When I write cat a>a The following error message is displayed cat: a: input file is output file and my file a is truncated to zero size. Also the exit status of the last command is 1 Can someone tell me what actually happens when I do so? (1 Reply)
Discussion started by: aagajaba
1 Replies

7. Shell Programming and Scripting

Remote server file output redirection

Hi, I want ssh to the remote server and then execute ls and redirect the output to the file in remote server itself like ssh root@$server `ls /var/log/users.txt > root@$server:/home/users.txt` Can you please let me know the correct syntax for it. Thanks in advance (2 Replies)
Discussion started by: mohitmoudgil
2 Replies

8. UNIX for Dummies Questions & Answers

Convert a tab delimited/variable length file to fixed length file

Hi, all. I need to convert a file tab delimited/variable length file in AIX to a fixed lenght file delimited by spaces. This is the input file: 10200002<tab>US$ COM<tab>16/12/2008<tab>2,3775<tab>2,3783 19300978<tab>EURO<tab>16/12/2008<tab>3,28523<tab>3,28657 And this is the expected... (2 Replies)
Discussion started by: Everton_Silveir
2 Replies

9. UNIX for Dummies Questions & Answers

Redirection of output to a log file

Apologies for the trivial nature of this question but I cannot seem to get a simple re direct to a log file to work Step 1 touch log.txt at -f batch.sh now >> log.txt I am trying to get the batch.sh contents into the log file Manny Thanks (8 Replies)
Discussion started by: JohnCrump
8 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question