syslog is the protocol as well as application to send message to Linux system logfile located at /var/log directory.
Sysklogd provides two system utilities which provide support for system logging and kernel message trapping.
Usually most program and apps use C or syslog application / library sending syslog messages.
But how do you send message from a shell prompt or shell script?
Use logger command which is a shell command interface to the syslog system log module. It makes or writes one line entries in the system log file from the command line.
Log message System rebooted for hard disk upgrade
$ logger System rebooted for hard disk upgrade
You can see message in /var/log/message file
# tail -f /var/log/message
Jan 26 20:53:31 dell6400 logger: System rebooted for hard disk upgrade
You can use logger command from a shell script. Consider 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 ] && /bin/rm $ATTCH || : MTO="email@example.com" for db in $HDBS do FILE="$BAK/$db.$NOW-$(date +"%T").gz" mysqldump -u admin -p'password' $db | gzip -9 > $FILE done tar -jcvf $ATTCH $BAK mutt -s "DB $NOW" -a $ATTCH $MTO <<EOF DBS $(date) EOF [ "$?" != "0" ] && logger "$0 - MySQL Backup failed" || :
Last line will log a message in /var/log/message file if backup failed.
To log a message contained in the /var/log/myapp.log file, use:
$ logger -f /var/log/myapp.log
Log the message to standard error (screen), as well as the system log:
$ logger -s "Hard disk full"
Refer to the man page for more options:
- 30 Handy Bash Shell Aliases For Linux / Unix / Mac OS X
- Top 30 Nmap Command Examples For Sys/Network Admins
- 25 PHP Security Best Practices For Sys Admins
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- Linux: 20 Iptables Examples For New SysAdmins
- Top 20 OpenSSH Server Best Security Practices
- Top 20 Nginx WebServer Best Security Practices
- 20 Examples: Make Sure Unix / Linux Configuration Files Are Free From Syntax Errors
- 15 Greatest Open Source Terminal Applications Of 2012
- My 10 UNIX Command Line Mistakes
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- The Novice Guide To Buying A Linux Laptop