Linux Command Repeating in Tuxville

LinuxLinuxBeginner
Practice Now

Introduction

In the bustling medieval city of Tuxville, known for its vast markets and intricate craftsmanship, there lived a humble farmer named Gregor. One day, he overheard the royal cartographers saying they needed someone who could monitor the growth progress of the kingdom's crops and report it every hour. Considering this a golden opportunity to serve his kingdom and earn some silver coins, Gregor decided to venture into learning the craft of consistent observation with a bit of magic from the Linux realm. He needs your help to master the command that could repeat itself to watch over the crop fields tirelessly.


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-271434{{"Linux Command Repeating in Tuxville"}} end

Understanding the watch Command

In this step, you're going to help Gregor learn how to use the watch command to monitor the status of his crops.

Tasks

  • Task 1: Learn the syntax of the watch command and its basic operation.
  • Task 2: Use the watch command to repeatedly list the contents of the directory where crop reports are stored.

Requirements

  • Familiarize yourself with the man pages to understand how the watch command works.
  • The default working path for terminal operations is /home/labex/project, and the relative path is ~/project.
  • Store the crop reports in a directory named crops inside the working path.
  • Use the watch command to monitor the crops directory.
  • Command instructions must be executed in the zsh terminal.

Example

Before using watch, Gregor's directory looks like this:

ll /home/labex/project/crops
total 0

After using watch, You can open a new terminal and create some files in the crops directory. The watch command will display the contents of the directory every 2 seconds.

โœจ Check Solution and Practice

Generating a Report with Timestamps

In this step, Gregor will generate a report file with timestamps using the watch command.

Tasks

  • Task 1: Write a script that appends a new timestamp entry to a file called report.txt inside the crops directory every 1 second.
  • Task 2: Use the watch command to execute this script repeatedly.

Requirements

  • Create a script named timestamp.sh inside the ~/project/crops directory. Use the date command inside the script to get the current timestamp.
    #!/bin/bash
    echo "$(date)" >> /home/labex/project/crops/report.txt
  • The report.txt file should contain the history of executed timestamps.
  • Utilize the watch command to run the script every 1 second.

Example

After using the watch command to continuously execute the timestamp.sh script, the report.txt file will be updated with a new timestamp every second.

You can verify that the watch command is working correctly by executing the following script:

cat /home/labex/project/crops/report.txt

The output should look like this:

Sat Aug 12 09:00:01 UTC 2023
Sat Aug 12 09:02:01 UTC 2023
Sat Aug 12 09:04:01 UTC 2023
โœจ Check Solution and Practice

Summary

In this challenge, you were tasked to guide Gregor in mastering the watch command to keep a vigilant eye over his crops. By utilizing this powerful command, you successfully helped him generate and continuously update a crop report that satisfies the royal cartographers' demands. Not only did you enrich your Linux command line prowess, but you also contributed to the well-being of Tuxville's agricultural excellence.