Creating Syslog entries and generating an alarm using a bash script

by | Feb 25, 2021 | BASH

In this article, we are going to discuss the syslog protocol. We’ll also learn about the logger command, which is a shell command and acts as an interface for the syslog module. The logger command makes entries in the system log. In this section, we are also going to create an alarm using a script.

Prerequisites

Besides having a terminal open, we need to make sure you have a file to make an entry.

Write Script:

We are going to use the logger command to enter file_name into the syslog file. Run the following command:

$ logger -f file_name

Now we are going to write a script to create an alarm. Create a create_alarm.sh script and write the following code in it:

create_alarm.sh

#!/bin/bash 
declare -i H 
declare -i M 
declare -i cur_H 
declare -i cur_M 
declare -i min_left 
declare -i hour_left 
echo -e "What time do you Wake Up?" 
read H 
echo -e "and Minutes?" 
read M 
cur_H=`date +%H` 
cur_M=`date +%M` 
echo "You Selected " 
echo "$H:$M" 
echo -e "\nIt is Currently $cur_H:$cur_M" 
if [ $cur_H -lt $H ]; then 
	hour_left=`expr $H - $cur_H` 
	echo "$H - $cur_H means You Have: $hour_left hours still" 
fi 
if [ $cur_H -gt $H ]; then 
	hour_left=`expr $cur_H - $H` 
	echo -e "\n$cur_H - $H means you have $hour_left hours left \n" 
fi 
if [ $cur_H == $H ]; then 
	hour_left=0 
	echo -e "Taking a nap?\n" 
fi 
if [ $cur_M -lt $M ]; then 
	min_left=`expr $M - $cur_M` 
	echo -e "$M -$cur_M you have: $min_left minutes still" 
fi 
if [ $cur_M -gt $M ]; then 
	min_left=`expr $cur_M - $M` 
	echo -e "$cur_M - $M you have $min_left minutes left \n" 
fi 
if [ $cur_M == $M ]; then 
	min_left=0 
	echo -e "and no minutes\n" 
fi 
echo -e "Sleeping for $hour_left hours and $min_left minutes \n" 
sleep $hour_left\h 
sleep $min_left\m 
mplayer ~/.alarm/alarm.mp3

How script works

Now we will see a description of the logger command and the create_alarm script we just wrote:

  • The logger command made an entry about your file in the syslog file, which was in the /var/log directory of your system. You can check that file. Navigate to the /var/log directory and run nano syslog and you will find the entry in that file.
  • We created a script to create an alarm. We used the date command for the date and time. We also used the sleep command to block the alarm for that particular time.