Home » BASH » Creating Syslog entries and generating an alarm using a bash script

Creating Syslog entries and generating an alarm using a bash script

Last updated Oct 5, 2021

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.

Related Posts

Creating a config file and using it in tandem with your scripts

In this article, we are going to create a config file and use it in our shell script.PrerequisitesBesides having a terminal open, you need basic knowledge of creating scripts and config files.Write scriptNow, we are going to create a script and config file. The...

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

Using Bash to monitor battery life and optimize it

In this article, we will learn about the TLP Linux tool. TLP is a command-line tool; it is used for power management and will optimize the battery life.PrerequisitesBesides having a Terminal open, you need to ensure that you have TLP installed on your system.How to do...

Creating a simple NAT and DMZ firewall using bash script

In this article, we will create a simple NAT firewall with DMZ using iptables.PrerequisitesBesides having a Terminal open, you need to ensure that iptables is installed in your machine.Write scriptWe will write a script to set up a DMZ using iptables. Create...

Follow Us

Our Communities

More on BASH

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

two × two =

Shares