Shell scripting: Write message to a syslog / log file

by on January 26, 2007 · 13 comments· LAST UPDATED January 27, 2007

in , ,

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

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 13 comments… read them below or add one }

1 Kate Ward March 14, 2007 at 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.

Reply

2 unknown123 February 27, 2008 at 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

Reply

3 abby August 4, 2009 at 1:56 am

i like how you make starting, i love it

Reply

4 Kongo Ge August 11, 2009 at 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

Reply

5 abby September 5, 2009 at 1:29 pm

i liked your website can you make new backgrounds please

Reply

6 anonymous coward November 14, 2009 at 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.

Reply

7 Tony July 21, 2010 at 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

Reply

8 nixCraft July 22, 2010 at 12:32 am

You need to use incrond.

Reply

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

in scripts I use inotify tools

Reply

10 RemikP April 1, 2011 at 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?

Reply

11 Dhruv January 8, 2012 at 7:55 am

i really like this…

Reply

12 Rayan July 29, 2012 at 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

Reply

13 john fraize October 17, 2012 at 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

Reply

Leave a Comment

Previous post:

Next post: