logger command to write a message to a Syslog daemon called Rsyslogd log file under Linux, macOS, *BSD or Unix.
Log message System rebooted for hard disk upgrade
logger "System rebooted for hard disk upgrade"
You can see message in /var/log/message file
sudo tail -f /var/log/message
OR
sudo tail -f /var/log/syslog
To log a message contained in the /var/log/myapp.log file, use:
sudo logger -f /var/log/myapp.log
Log the message to standard error (screen), as well as the system log:
sudo logger -s "Hard disk full"
State a priority using the -p option and tag the line with a specified tag with help of -t option. For instance, here is how to log message on a Linux machine to a Syslog:
logger -p {priority} -t {tag} "message ..." logger -p local0.notice -t "Backup script" "The database backups successfully to FreeBSD NAS server."
How to send message to a remote syslog server? Use the netcat or nc command. The syntax is as follows for the combination of echo command or nc command:
echo "myapp.sh: This is a test message" | nc -w1 -u lan1.cyberciti.biz 514 echo "myapp.sh: This is a test message" | nc -w1 -u 192.168.2.100 514
Where nc command options are:
-w1 : Terminate after receiving recvlimit packets from the network. In this case 1. -u : Use UDP instead of TCP. In other words send message using UDP at port 514 lan1.cyberciti.biz or 192.168.2.100 : Your remote syslog server IP or FQDN. 514 : UDP port 514 for your remote syslog host. Send syslog message from command line using bash If you are using an updated version of bash, then try the following syntax:
echo "myapp.sh: This is test" \
/dev/udp/lan1.cyberciti.biz/514
## OR use the IP ## echo "myapp.sh: This is test message from BASH " \
/dev/udp/192.168.2.100/514
One can use the bash for loop to send message to multiple Unix or Linux servers:
for s in 192.168.1.100 192.168.1.101 do
echo "$0: RAID hardware failure detected on ${HOSTNAME} @ $(date). Please change RAID card." > "/dev/udp/${s}/514"
done Summing up This quick tip explained how to write an entry into /var/log/syslog from the command-line or custom-made shell script when using Linux or Unix machine