write the output of a command to /var/log/user.log… each line will contain $USER, making this easy to grep for.

write the output of a command to /var/log/user.log… each line will contain $USER, making this easy to grep for.

1
 log() { (echo "$ [email protected]";[email protected]) | logger -t $USER; }

This command is useful if you want to copy the output of a series of commands to a file, for example if you want to pastebin the output from ‘uname -a’, ‘lspci -vvv’ and ‘lsmod’ for video driver trouble-shooting on your favorite Linux forum.

‘log’ takes all the following arguments as a command to execute, with STDOUT sent to /var/log/user.log. The command is echoed to the log before it is executed.

The advantages of using logger (as opposed to appending output from commands to a file) are 1) commands are always appended to the logs… you don’t have to worry about clobbering your log file accidentally by using ‘>’ rather than ‘>>’ 2) logs are automatically cleaned up by logrotate.

The following functions allow you to mark the start and end of a section of /var/log/user.log.

1
startlog() { export LOGMARK=$(date +%Y.%m.%d_%H:%M:%S); echo "$LOGMARK.START" | logger -t $USER; }

then

1
endlog() { "$LOGMARK.END" | logger -t $USER; }

printlog will print all lines between $LOGMARK.START and $LOGMARK.END, removing everything that is prepended to each line by logger.

1
printlog() { sudo sed -n -e "/$LOGMARK.START/,/$LOGMARK.END/p" /var/log/user.log| sed "s/.* $USER: //"; }

Caveats: I’m sure that those could use some cleanup and error checking… there are unchecked dependencies between printlog and endlog, as well as between endlog and startlog.

It might be useful for ‘log’ to also send stderr to logger as well.

* View this command to comment, vote or add to favourites * View all commands by bartonski

commandlinefu.com

by David Winterbottom (codeinthehole.com)

URL: http://feedproxy.google.com/~r/Command-line-fu/~3/6DP_QDTn2IM/write-the-output-of-a-command-to-varloguser.log…-each-line-will-contain-user-making-this-easy-to-grep-for.

Enhanced by Zemanta

Logwatch for Linux Systems – Scheduled email.

On my personal desktop at home, I like to see the statistics at least once a day, for what was installed, what was run with sudo and other such details like kernel errors.

Running this monotonously every day is quite boring, so comes to rescue is logwatch. I have Fedora installation so I will talk about the location with respect to that so for your distribution it might be a little different.

First install logwatch using 🙂 yum install logwatch

and you are done.

If you need to do more configuration and want to see something different in the mail that is sent( BTW sendmail is assumed to be default mail client), the keep reading.
First open the file /etc/logwatch/conf/logwatch.conf
The contents for my distro is just one line 🙂
# Local configuration options go here (defaults are in /usr/share/logwatch/default.conf/logwatch.conf)
So, open the file /usr/share/logwatch/default.conf/logwatch.confbr
I will highlight the important lines in the file here:
LogDir = /var/log — This is the directory for all the log files
MailTo = root — Whom should the mail be sent to
Print = If this is set to true then there will be no mail sent and the output will be displayed on the stdout.
Detail = The level of details you want to see in the mail or the output on screen.

Thats pretty much it.. If you want to further modify the details in the mail you can configure the services. For more advanced usage you can even go to /usr/share/logwatch/scripts/services and configure the individual scripts.

\"Reblog