[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: logging a bash script using sudo and time



On Wed, Sep 24, 2008 at 10:19 AM, Eugene V. Lyubimkin
<jackyf.devel@gmail.com> wrote:
> Brian McKee wrote:
>> I regularly run a script with time and sudo.  e.g.
>>> time sudo echo 'hi mom'
> [snip]
>> Now I want to log the entire output to a log file as well as display
>> it on the screen.
>>> time sudo echo 'hi mom' | tee | logger -f /var/log/hiMom
>> Only that doesn't work :-)  Time is only on the screen, not in the log
>> file, and /var/log/hiMom is empty, regardless of the permissions on
>> that file.
>> Can someone explain the redirection going on here in a way I can grok?

> 'time' writes output to stderr, use "time sudo echo 'hi mom' 2>&1"
> And why "logger -f"? Just "tee /var/log/hiMom".

Aha!  Thanks - I didn't know time used stderr and I didn't think about
that being a possibility.

I have logger in there because I was considering whether to put the
output in syslog, or a file in /var/log, and either way I like the
formatting you get with the -i option.

Now I've really confused myself.  I ran it without the tee on three
different systems (buntu, Mandrake, a nameless proprietary OS) and got
three different answers!

---
==> time sudo echo 'hi mom' 2>&1 | logger -i -f /var/log/test.log

real    0m0.018s
user    0m0.000s
sys     0m0.016s

==> cat /var/log/test.log

---
==> time sudo echo 'hi mom' 2>&1 | logger -i -f /var/log/test.log

==> cat /var/log/test.log

---
==> time sudo echo 'hi mom' 2>&1 | logger -i -f /var/log/test.log
Password:

real    0m2.835s
user    0m0.004s
sys     0m0.014s

==> cat /var/log/test.log
hi mom

---

Poking around seems to indicate that the -i option doesn't work when
used in combination with the -f option,  but I can't explain the
differences in output past that.   Jeez, I thought this would be easy!

Brian


Reply to: