LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Customizing STDERR redirection to a file (https://www.linuxquestions.org/questions/linux-newbie-8/customizing-stderr-redirection-to-a-file-920826/)

cthugr 12-28-2011 12:51 AM

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


porphyry5 12-28-2011 06:55 AM

Quote:

Originally Posted by cthugr (Post 4559785)
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?

David the H. 12-28-2011 07:03 AM

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 `..`.

cthugr 12-28-2011 07:32 AM

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)


All times are GMT -5. The time now is 01:37 AM.