Article Title: Introduction to common log management techniques for Linux operating systems. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
1. Use Shell to write information to the Syslog Log File
The log file (in the/var/log directory) that the application uses the Syslog protocol to send information to the Linux system ). Sysklogd provides two system tools: system logging and kernel information capturing. Generally, most programs use the C language, Syslog application, or library to send Syslog messages.
The following describes how to use sShell to write information to a Syslog log file:
(1). Use the Logger command
The Logger command is a Shell command (interface ). You can use this interface to use the Syslog System Log Module. You can also directly write a line of information to the system log file from the command line.
For example, record the system restart information after the hard disk upgrade:
$ Logger System rebooted for hard disk upgrade
Then you can view the/var/log/message file:
# Tail-f/var/log/message
Output:
Jan 26 20:53:31 dell6400 logger: System rebooted for hard disk upgrade
You can also use the Logger command through a script program. See the following example:
#! /Bin/bash
HDBS = "db1 db2 db3 db4"
BAK = "/sout/email"
[! -D $ BAK] & mkdir-p $ BAK |:
/Bin/rm $ BAK /*
NOW = $ (date + "% d-% m-% Y ")
ATTCH = "/sout/backup. $ NOW. tgz"
[-F $ ATTCH] & amp;/bin/rm $ ATTCH |:
MTO = "you@yourdomain.com"
For db in $ HDBS
Do
FILE = "$ BAK/$ db. $ NOW-$ (date +" % T "2.16.gz"
Mysqldump-u admin-p 'Password' $ db | gzip-9> $ FILE
Done
Tar-jcvf $ ATTCH $ BAK
Mutt-s "DB $ NOW"-a $ ATTCH $ MTO <DBS $ (date)
EOF
["$? "! = "0"] & logger "$0-MySQL Backup failed" |:
If the MySQL database backup fails, the last line of code above will write a message to the/var/log/message file.
(2) other usage
If you need to record the information in the/var/log/myapp. log file, you can use:
$ Logger-f/var/log/myapp. log
Send messages to the screen (standard errors), such as system logs:
$ Logger-s "Hard disk full"
[1] [2] Next page