nixCraft Poll

Topics

Shell scripting: Write message to a syslog / log file

Posted by Vivek Gite [Last updated: January 27, 2007]

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?

logger command

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
Output:

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="you@yourdomain.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.

Other usage

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:
man logger
man syslogd

Want to stay up to date with the latest Linux tips, news and announcements? Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

You may also be interested in other helpful articles:

Discussion on This Article:

  1. Ricardo Martins » Blog Archive » Script Shell: enviando mensagens para o syslog ou arquivo de log Says:

    [...] cyberciti.biz, demonstra os passos para enviar mensagens de log de um prompt ou scritp shell… [ link ] mensagens Scripts shell link syslogConverter em [...]

  2. Kate Ward Says:

    If you want to log to a remote Syslog server, the logger command will not work as it logs only to the syslogd on the localhost. log4sh is a logging framework for shell scripts that *can* log to a remote Syslog server as long as netcat is installed.

  3. unknown123 Says:

    the following code is not writing either of the messagesto /var/log/messages.What is wrong in the code?

    My file is saved at /usr/local/sbin location.example is the name of my file.

    if [ -e /usr/local/sbin/example ]; then
    logger File exists
    else
    logger File does not exist
    fi

    I have added the above code in /etc/rc.local file.

    Thankyou in advance

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Copyright © 2004-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.