Linux Command Repeating

LinuxLinuxBeginner
Practice Now

Introduction

In the heart of ancient Egypt, within the grand palaces of a powerful Pharaoh, there lies an extraordinary chamber adorned with frescoes that tell the stories of ages past. In this chamber, works an esteemed court painter whose mastery of art has granted him both fame and favor in the eyes of the ruler. The painter's latest commission involves capturing the grandeur of Pharaoh's latest victories and daily life. However, to ensure accuracy and a constant stream of updates to his work, the painter must rely on the distant cousin of his brushes and chisels: the command watch.

The goal of this Lab is to master the use of the watch command, ensuring that the painter can stay updated with the latest developments in Pharaoh's realm. The command will let the painter repeatedly observe the changes, just like how he captures life's continuous flow onto his papyrus canvas.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/SystemInformationandMonitoringGroup -.-> linux/watch("`Command Repeating`") subgraph Lab Skills linux/watch -.-> lab-271435{{"`Linux Command Repeating`"}} end

Exploring the watch Command

In this step, you'll learn the basics of the watch command. The watch command is used to run any designated command at regular intervals, displaying its output on your terminal. This is incredibly useful for monitoring the progress or updates of a running process.

Imagine you are the painter, and you want to keep a check on the hourly temperature of the Nile as it can affect the drying of your paints. To simulate this, let's create a simple script that outputs a random temperature every time it is run:

First, navigate to your working directory:

cd ~/project

Then, create the script generate_temperature.sh with the following content.

The content of the script:

echo $(((RANDOM % 50) + 10)) 'degrees Celsius' > temperature.txt

To observe the temperature changes using watch, we would execute the command:

watch -n 3600 bash ./generate_temperature.sh

This command will execute generate_temperature.sh every hour (3600 seconds) and display the recorded temperature.

Watching Directories for Changes

In this step, you will simulate monitoring changes in the design drafts that are kept in a directory. As the paintings evolve, you must keep an eye on the designs updated by your apprentices.

First, let's create a directory to simulate the drafts storage and a dummy draft file:

mkdir ~/project/drafts
touch ~/project/drafts/draft1.txt

To see real-time updates in the drafts directory, you would use watch with the ls command:

watch -n 30 ls -l ~/project/drafts

This would execute ls -l on the drafts directory every 30 seconds, showing you the latest changes in file sizes or modification times.

Summary

In this lab, the focus was to transpose you into the role of a vigilant Egyptian painter tasked with keeping a close and constant eye on the evolving world around you, using the watch command as your tool. From checking the temperature on the banks of the Nile to keeping a watchful eye over your apprentice's work, the command watch emerges as a significant asset in your digital arsenal. Through carefully crafted scenarios, this lab has hopefully instilled a practical understanding of continuous monitoring and has set you on a path to becoming as attentive in the command line as our painter is on his canvas.

Other Linux Tutorials you may like