Introduction
In this lab, you will learn how to set up scheduled tasks on Linux. You'll explore two powerful tools: the watch command for running commands repeatedly at short intervals, and the crontab utility for scheduling tasks to run at specific times. These skills are essential for system administrators and anyone who needs to automate recurring tasks on a Linux system.
Use the watch Command
The watch command allows you to run a command repeatedly and view its output in real-time. This is useful for monitoring changes or running a command at regular short intervals.
Open your terminal. You should be in the
/home/labex/projectdirectory. If you're not sure, you can always check your current directory with thepwdcommand.We'll use
watchto display the current date and time every 5 seconds. Enter the following command:watch -n 5 dateHere's what this command does:
watchis the command we're using to repeat another command.-n 5is an option that tellswatchto wait 5 seconds between each execution. If you omit this,watchwill use a default interval of 2 seconds.dateis the command thatwatchwill run repeatedly. It displays the current date and time.
After entering the command, you should see a full-screen display that updates every 5 seconds. It will look similar to this:

The top line shows the command being run and how often it's updating. The rest of the screen shows the output of the
datecommand.To exit the
watchcommand, pressCtrl+C. This key combination is commonly used in Linux to terminate a running command.If you don't stop the
watchcommand, it will continue running indefinitely, which might prevent you from entering new commands in your terminal.
Install Crontab
Before we can use crontab to schedule tasks, we need to make sure it's installed on our system. Most Linux distributions come with crontab pre-installed, but it's good practice to check and install it if necessary.
First, let's check if crontab is already installed. We can do this by trying to run the crontab command:
crontab -lIf crontab is installed, you'll either see a list of your current cron jobs or a message saying "no crontab for labex".
If you see an error message saying the command is not found, we need to install crontab. On Ubuntu or Debian-based systems, we can install it using the following commands:
sudo apt update sudo apt install cronYou'll be prompted to enter your password. Type it in (note that you won't see any characters as you type for security reasons) and press Enter.
After installation, start the cron service:
sudo service cron startThis command starts the cron service immediately.
Now, verify that cron is installed and running:
sudo service cron statusYou should see output indicating that the cron service is running.
Introduction to Crontab
Now that we have crontab installed, let's explore how to use it. Crontab is used for scheduling tasks at specific times, even when you're not logged in.
First, let's view your current crontab entries (if any). Use the following command:
crontab -lThe
-loption stands for "list". This command displays all the scheduled tasks in your crontab.If you haven't set up any cron jobs yet, you'll see a message saying "no crontab for labex". This is normal for a new user.
Now, let's open the crontab for editing. Use this command:
crontab -eThe
-eoption stands for "edit". This command opens your crontab file in a text editor.If this is your first time using crontab, you may be prompted to choose an editor. You'll see a list of numbers corresponding to different editors. For beginners, nano (usually option 1) is a good choice because it's simpler to use. Type the number for nano and press Enter.

If you're not prompted to choose an editor, don't worry. It means a default editor has already been set up for you.
Once the editor opens, you'll see an empty file (or any existing cron jobs if you had any). Don't add anything yet; we'll do that in a later step. For now, just familiarize yourself with the editor.
If you're using nano, you'll see some help commands at the bottom of the screen. The
^symbol represents the Ctrl key. So^Xmeans "Ctrl+X", which is used to exit nano.Exit the editor without making any changes. In nano, you can do this by pressing Ctrl+X. If you're asked if you want to save changes, press 'N' for No.
Understanding Crontab Syntax
Before we add a cron job, it's crucial to understand the syntax. Crontab uses a specific format to determine when a task should run.
The basic format of a cron job is:
* * * * * command_to_executeEach asterisk represents a specific time unit, from left to right:
- Minute (0-59)
- Hour (0-23)
- Day of the month (1-31)
- Month (1-12)
- Day of the week (0-7, where both 0 and 7 represent Sunday)
Here are some examples to help you understand:
30 2 * * *means "At 2:30 AM, every day"0 9 * * 1-5means "At 9:00 AM, Monday through Friday"*/15 * * * *means "Every 15 minutes"
You can use the following special characters:
*: any value,: value list separator-: range of values/: step values
Let's break down a more complex example:
15,45 9-17 * * 1-5 /path/to/script.shThis means: "Run /path/to/script.sh at 15 and 45 minutes past every hour from 9 AM to 5 PM, Monday to Friday"
Understanding this syntax is key to creating effective cron jobs. Take some time to think about how you might schedule different tasks using this format.
Add a Cron Job
Now that we understand the syntax, let's add a simple cron job that writes the current date to a file every minute.
Open your crontab for editing:
crontab -eOnce the editor opens, add the following line to your crontab:
* * * * * date >> /home/labex/project/date_log.txt
Let's break this down:
- The five asterisks
* * * * *mean "every minute of every hour of every day of every month and every day of the week". dateis the command we're running.>>is used to append the output to a file (instead of overwriting it)./home/labex/project/date_log.txtis the file where we're storing the output.
- The five asterisks
Save and exit the editor. If you're using nano:
- Press
Ctrl+Xto exit - Press
Yto confirm that you want to save changes - Press
Enterto confirm the file name
- Press
After you exit, you should see a message saying "crontab: installing new crontab". This confirms that your changes have been saved.
Monitor Your Cron Job
Now that we've set up a cron job, let's check if it's working correctly.
First, we need to wait for at least one minute after adding the cron job. This is because cron jobs run on the minute, so it may take up to a minute for your job to run for the first time.
After waiting, use the
catcommand to view the contents of the log file:cat /home/labex/project/date_log.txtThe
catcommand displays the contents of a file directly in the terminal.You should see at least one line with a date and time. It will look something like this:
Sat Aug 5 10:15:01 UTC 2023If you don't see anything, wait another minute and try again. Sometimes it can take a moment for the cron job to start running.
To see the cron job in action, you can use the
watchcommand we learned earlier to monitor the file in real-time:watch -n 60 cat /home/labex/project/date_log.txtThis will update every 60 seconds, showing you the new entries as they're added.
Let this run for a few minutes. You should see a new line added each minute.
When you're done observing, press
Ctrl+Cto exit thewatchcommand.
Summary
Congratulations! You've successfully completed the "Set Up Scheduled Tasks" lab. You've learned how to:
- Use the
watchcommand to run commands repeatedly at short intervals. - Install and set up crontab on your system.
- View and edit your crontab using
crontab -landcrontab -e. - Understand crontab syntax for scheduling tasks.
- Create a simple cron job that runs every minute.
- Monitor the output of your cron job.
These skills are fundamental for automating tasks and monitoring systems in Linux environments. As you continue your Linux journey, you'll find many more uses for scheduled tasks in system administration and automation.
Remember, while we used a simple example of logging the date, you can use cron jobs for more complex tasks like backing up data, updating software, or running system maintenance scripts. Always be careful when setting up cron jobs, especially if they involve system-critical operations.



