Capture output of program to file with limited filesize


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capture output of program to file with limited filesize
# 1  
Old 04-12-2005
Capture output of program to file with limited filesize

I'm working on a script that will perform a backup, save a log of said backup and send the output to me in an email. Everything is working fine so far except that I can't figure out how to specify a maximum file size for the log file. I don't want a runaway log file jamming up the server. Currently, the command I'm using to perform the backup and capture output is:

Code:
/usr/local/bin/rsync -avz --eahfs $SOURCE_DIR $DEST_DIR &> $LOG_FILE

* "--eahfs" specifies support for HFS+ forks. This is running on OS X server 10.3

I'd appreciate any input. Thanks!
# 2  
Old 04-12-2005
How could you do that? You have no way of knowing if a shortened logfile will have the information you want in it.

You could split the output and only send the error log. That might help.
# 3  
Old 04-12-2005
Well, I was thinking that a max filesize of a meg would allow any non-redundant information, but I suppose that just sending any error information would work better. I'd still like to find a way to cap it, though, just for safety's sake. Is that possible?
# 4  
Old 04-13-2005
I can see both sides of this. reborg has a valid point. If you cap a log file at n lines, you run the risk that line n+1 will be critical information. On the other hand, several times a year I get dragged out of bed because because a server crashed with /var filled due to something like a 5 GB /var/tmp/some_dumb_program.log. It's hard to carefully peruse a 5 GB text file, especially at 3 in the morning, but it's usually my sense that capping the file at, say, half a Gig would not have been a tragedy.

As for if it can be done, yes, but with some caveats. One idea is to create a separate filesystem, say, /scratch. If /scratch is 1 GB, put the file there and it won't exceed 1 GB. You might not have the resources to create a separate filesystem though.

Another option is ulimit, but there is a danger here and a possible obstacle. Your syntax is using &> file which is syntax that I have never seen. But that is close to >& file which would be csh syntax. My csh does not have a ulimit command. Both ksh and bash do. But they need a ulimit facility in the kernel to do that. I don't know if darwin has ulimit. If it does you will need to you a shell with ulimit support. Switching shells is attractive for other reasons: Csh Programming Considered Harmful That's the obstacle...can you use ulimit?

As for the danger, you don't want to simply toss a "ulimit -f 100" or whatever at the top of your script. rsync may need to write a file larger than that just as it syncs. I'm not sure if ulimit affects sockets, but my reading of "man write" suggests that it might. If so, even if the local rsync is reading files and sending them to another system, its socket could be capped. So you want to limit only the log file. In ksh, this should be safe....
rsync options 2>&1 | (ulimit -f 100 ; cat > rsync.log)
The ulimit happens in a subshell which then runs a cat command and the cat command will be limited in the size of file it can create.
# 5  
Old 04-13-2005
As for my syntax, I apologize I'm new to shell programming and that was just a mistake. I was trying to work out how to redirect specific output and the only site I found had something similar to that in an example. I'm working in bash right now, so ulimit seems like the way to go. I'll do some more searching on the subject. Your solution looks good, but if it's ksh specific it won't work for me at the moment.

Thanks for the input. I really appreciate it. I'll post back with what I end up doing Smilie

Edit: One other thing I just thought of... what if I was to do somethign like:
Code:
/usr/local/bin/rsync -avz --eahfs $SOURCE_DIR $DEST_DIR | tail -$LINES > $LOG_FILE

I'm not sure how to work stderr into it, though...

Last edited by spectre_240sx; 04-13-2005 at 03:49 PM..
# 6  
Old 04-13-2005
2>&1 | tail
would work stderr into it.

But what if there is a gigabyte line?
# 7  
Old 04-13-2005
Since you are usinng bash the same syntax will work.

Code:
/usr/local/bin/rsync -avz --eahfs $SOURCE_DIR $DEST_DIR 2>&1 | tail -$LINES > $LOG_FILE

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Capture output of open pipe to a file in perl

Hi, I am trying to capture the output of the an open pipe in perl. but I am not sure how to do this. can some one please help me do that? Below is the script I am using (2 Replies)
Discussion started by: ahmedwaseem2000
2 Replies

2. Shell Programming and Scripting

how to invoke external program and capture its output

Hi all, I am using an external binary to view memory starting from a specific address and i want to automate this via PERL however there are problems. Hope you can help me ..thx The output of the programme is like below: bash-3.2$ mem_disp 12B21D20 100 Opening RO Data Memory File scp.ro... (4 Replies)
Discussion started by: ekckabatop
4 Replies

3. Shell Programming and Scripting

Want ro capture the debug in output file

I want to capture the debug for the below command in output file . i tried like this but its not working: sh -xv <scriptname> >> output.log i want the output in a log file. Anyone plz help in this (2 Replies)
Discussion started by: chakkaravarthy
2 Replies

4. Shell Programming and Scripting

How to capture output to log file

Hi I have a script that will run multiple unix & sql commands. I want to see the output as well as capture it to a log file for further analysis. Is there an easy way to do that instead of adding "tee -a logfile" on everyline or even on the execute line (i.e. script | tee -s logfile). Thanks (1 Reply)
Discussion started by: nimo
1 Replies

5. Shell Programming and Scripting

How to capture RC from SAS Program into K-shell

I am running a SAS program within K-shell. This SAS program gives me return code of 3000. However, when I check return code in my K-shell, it displays RC=2. Can someone tell me how to capture RC from SAS to K-shell? Thanks. Pramodini (2 Replies)
Discussion started by: Pramodini Rode
2 Replies

6. Programming

capture key board events using C program

hi This is mahesh please can any one post source of 'c'. to capture key board events. thank u (1 Reply)
Discussion started by: smahesh2007
1 Replies

7. Shell Programming and Scripting

Capture Shell Script Output To A File

Hi, I am running a shell script called dbProcess.sh which performs shutdown and startup of various Oracle instances we have.At the time of execution the script produces the following output to the command line window $./dbProcess.sh stop #### Run Details ###### Hostname : server-hop-1... (4 Replies)
Discussion started by: rajan_san
4 Replies

8. Shell Programming and Scripting

capture output of file and send last string thereof to new file

Hello, If I run a program from within shell, the output is displayed in the command line terminal. Is there a way I can capture that output and choose only the very last string in it to send it to a new file? Thank you (6 Replies)
Discussion started by: Lorna
6 Replies

9. UNIX for Advanced & Expert Users

Capture output to file and printer

Hi All : I wanted a unix command by which I could be able to print the output to a file and at the same time to a printer. Any help will be greatly appreciated. Regards, Ramamurthy Dasari (1 Reply)
Discussion started by: rdasari
1 Replies

10. UNIX for Advanced & Expert Users

Implementing "Time Limited, Trial" version of my program

Hello everyone, I am just looking for ideas on how I can implement a demo version of my program which will be available in unix environment (solaris and linux). So what I have in mind is to either implement a limited-time version (trial version expires after 30 days) or a limited-number... (4 Replies)
Discussion started by: the_learner
4 Replies
Login or Register to Ask a Question