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

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:

HDBS="db1 db2 db3 db4"
[ ! -d $BAK ] && mkdir -p $BAK || :
/bin/rm $BAK/*
NOW=$(date +"%d-%m-%Y")
[ -f $ATTCH ] && /bin/rm $ATTCH  || :
for db in $HDBS
 FILE="$BAK/$db.$NOW-$(date +"%T").gz"
 mysqldump -u admin -p'password' $db | gzip -9 > $FILE
tar -jcvf $ATTCH $BAK
 mutt -s "DB $NOW" -a $ATTCH $MTO <<EOF
DBS $(date)
[ "$?" != "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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 17 comments so far... add one
CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
17 comments… add one
  • Kate Ward Mar 14, 2007 @ 14:17

    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.

    • Paul M Dec 3, 2015 @ 15:19

      logger also supports the “-n” option to send log messages to another node.

  • unknown123 Feb 27, 2008 @ 14:35

    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
    logger File does not exist

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

    Thankyou in advance

  • abby Aug 4, 2009 @ 1:56

    i like how you make starting, i love it

  • Kongo Ge Aug 11, 2009 @ 9:19


    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.


  • abby Sep 5, 2009 @ 13:29

    i liked your website can you make new backgrounds please

  • anonymous coward Nov 14, 2009 @ 23:11

    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 Jul 21, 2010 @ 23:37

    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.


    path modified



  • RemikP Apr 1, 2011 @ 14:04

    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 Jan 8, 2012 @ 7:55

    i really like this…

  • Rayan Jul 29, 2012 @ 10:56


    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 Oct 17, 2012 @ 15:08

    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 Jun 18, 2014 @ 15:28

    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 Oct 26, 2014 @ 14:59

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

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

  • rudi Oct 26, 2014 @ 15:19

    If you want to format the logger output:

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

    you have to use

    logger -t MYAPPLICATION -i

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum