LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-28-2011, 12:51 AM   #1
cthugr
LQ Newbie
 
Registered: Jan 2011
Posts: 11

Rep: Reputation: 0
Customizing STDERR redirection to a file


Hi,

I'm trying to create a small shell script that will use the rsync command to synchronize 2 directories and also will log to a file the success or not of the command and to another file the errors (not active network connection, etc). I'm stuck with the 2nd file, where I want to have the date and in a new line the output of standard error.

So far, I've done something like this:

Code:
if (rsync source destination) 2> /home/user/errors
then
   echo "`date` - Success" >> /home/user/status
else
   echo "`date` - Failure" >> /home/user/status
 
Old 12-28-2011, 06:55 AM   #2
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch
Posts: 445

Rep: Reputation: 21
Quote:
Originally Posted by cthugr View Post
Hi,

I'm trying to create a small shell script that will use the rsync command to synchronize 2 directories and also will log to a file the success or not of the command and to another file the errors (not active network connection, etc). I'm stuck with the 2nd file, where I want to have the date and in a new line the output of standard error.
I think this should demonstrate what you want.
Code:
~ $mkdir j
~ $mkdir k
~ $echo qwert > j/a
~ $> j/b
~ $> k/b
~ $if ! rsync -a j/*.* k 2> ~/errors; then echo "$(date) - Failure" >> ~/status; else echo "$(date) - Success" >> ~/status; fi
~ $cat errors
rsync: link_stat "/home/g/j/*.*" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.8]
~ $cat status
Wed Dec 28 05:59:49 PST 2011 - Failure
~ $if ! rsync -a j/ k 2> ~/errors; then echo "$(date) - Failure" >> ~/status; else echo "$(date) - Success" >> ~/status; fi
~ $cat errors
~ $cat status
Wed Dec 28 05:59:49 PST 2011 - Failure
Wed Dec 28 06:02:14 PST 2011 - Success
~ $ls j
a  b
~ $ls k
a  b
Did you find this post helpful?

Last edited by porphyry5; 12-28-2011 at 07:03 AM.
 
Old 12-28-2011, 07:03 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959Reputation: 1959
Assuming I understand you correctly, after the echo, just do this:
Code:
cat /home/user/errors >> /home/user/status
You may be trying to reinvent the wheel here though. There are doubtless plenty of ready-made rsync scripts already available on the web.

You might also have a look at the unison file synchronizer: http://www.cis.upenn.edu/~bcpierce/unison/

PS: $(..) is highly recommended over `..`.
 
Old 12-28-2011, 07:32 AM   #4
cthugr
LQ Newbie
 
Registered: Jan 2011
Posts: 11

Original Poster
Rep: Reputation: 0
Thank you for your answers!

I've already managed to what you're proposing. What I need in the end are two files:

1) status: this should contain the date and the status of the last operation, e.g. 28/12/2011 - Success or 28/12/2011 - Failure
2) errors: this should contain the date and the error, in case there is one, e.g. 28/12/2011 - rsync: file not found (or something like this)
 
  


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
bash: redirect stderr to file including name of file with the error SilversleevesX Programming 11 07-27-2010 03:06 PM
redirection stderr ? jackmor Linux - Desktop 2 03-15-2008 12:17 AM
redirecting stderr to file SPF Linux - Software 4 12-17-2007 06:56 AM
how do i redirect stderr to a file? nodger Programming 5 11-28-2004 11:22 AM
stderr redirection evilop Linux - Newbie 3 09-21-2004 04:27 PM


All times are GMT -5. The time now is 02:06 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration