≡ Menu

Shell scripting: Write message to a syslog / log file

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

Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

Comments on this entry are closed.

  • Kate Ward March 14, 2007, 2:17 pm

    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.

  • unknown123 February 27, 2008, 2:35 pm

    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

  • abby August 4, 2009, 1:56 am

    i like how you make starting, i love it

  • Kongo Ge August 11, 2009, 9:19 am

    Hi,

    I like the script very much, but I cannot figure out some parts.. :(
    Could you explain what “|| : ” means at the and of three lines, please.
    It must be POSIX mode, but what is it doing?

    Now I know what “&&” and “II” do.

    Thanks,
    Ge

  • abby September 5, 2009, 1:29 pm

    i liked your website can you make new backgrounds please

  • anonymous coward November 14, 2009, 11:11 pm

    To the poster above who’s rc.local script doesn’t work – if it works when you execute rc.local yourself, then its probably because the PATH isn’t set at that point, and you’ll need to specify the full path to logger.

  • Tony July 21, 2010, 11:37 pm

    I have this plist file that watches the “WatchDirectory” in my home directory for any changes to files or added files, scripts runs, or files deleted. If a change occurs, it calls the /usr/bin/logger command which basically writes to the /var/log/system.log file. All it writes though is very vague every time, nothing specific: for example

    “Jul 21 19:34:41 TonyMAC Tony[16512]: path modified”

    Do you know how I can have it write more specific messages like, “a file was deleted”, a script was ran or even a simple – “the change happened in this directory” to the /var/log/system.log file Thanks in advance.

    Label
    logger
    ProgramArguments

    /usr/bin/logger
    path modified

    WatchPaths

    /Users/Tony/WatchDirectory

    • nixCraft July 22, 2010, 12:32 am

      You need to use incrond.

    • Master Of Digital Puppets June 13, 2011, 11:57 am

      in scripts I use inotify tools

  • RemikP April 1, 2011, 2:04 pm

    According to manual, the -f switch is used to write specified file to the standard logfile (most probably messages.log), instead of what i understood from your post, choosing a logfile to write to.
    By the way, how can i tell logger to write to my file, instead of messages.log?

  • Dhruv January 8, 2012, 7:55 am

    i really like this…

  • Rayan July 29, 2012, 10:56 am

    Hello,

    I have custom log files in my home directory /home/user1/logs Can I use syslogd to forward these logs to another remote server ? I was able to forward logs from /var/log/

    Please advice. Thanks

  • john fraize October 17, 2012, 3:08 pm

    When I want to clear my /var/log/messages this works for me

    do this as root

    (optional backup your messages)
    tar -czvf /home/hunkingbigmessagefile.tgz /var/log/messages

    (the following is all required to clear a single log file)
    cat /dev/null > /var/log/messages

  • usman June 18, 2014, 3:28 pm

    what will be configuration if my livux machine is syslog client.

    how will i configure it to send syslog messages to any syslog server

  • rudi October 26, 2014, 2:59 pm

    Is it possible to format the logger output in bash?
    Jan 26 20:53:31 dell6400 logger: System rebooted for hard disk upgrade

    to
    Jan 26 20:53:31 dell6400 MYAPPLICATION: System rebooted for hard disk upgrade

  • rudi October 26, 2014, 3:19 pm

    If you want to format the logger output:

    from Jan 26 20:53:31 dell6400 logger: System rebooted for hard disk upgrade
    to
    Jan 26 20:53:31 dell6400 MYAPPLICATION[PID]: System rebooted for hard disk upgrade

    you have to use

    logger -t MYAPPLICATION -i