Introduction
In this lab, you will learn the fundamentals of task scheduling in a Linux environment. You will explore two essential command-line utilities: at for scheduling one-time jobs to run at a specific future time, and cron for automating recurring tasks that need to be executed on a regular schedule, such as daily, weekly, or monthly.
You will begin by preparing your lab environment, ensuring the at and cron services are installed and active. You will then practice creating a one-time job with the at command and managing the job queue with atq and atrm. Finally, you will use the crontab command to create, verify, and remove a recurring cron job, providing you with hands-on experience in automating system administration tasks.
Prepare the Lab Environment
In this step, you will prepare the lab environment by installing the necessary tools for scheduling tasks in Linux. The two primary tools we will be using are at for one-time tasks and cron for recurring tasks. While cron is typically pre-installed on most Linux systems, the at utility may need to be installed manually.
First, let's update the package list to ensure we can fetch the latest version of the software.
sudo apt-get update
You will see output as the package lists are downloaded from the repositories.
Next, install the at package using the apt-get command. The -y flag automatically confirms the installation, so you won't be prompted.
sudo apt-get install -y at
The output will show the progress of the installation. Once it's complete, the at command will be available on your system.
The at command depends on a background service (daemon) called atd to execute scheduled jobs. We need to ensure this service is running. You can check its status with the systemctl command.
sudo systemctl status atd
The output should indicate that the service is active (running).
● atd.service - Deferred execution scheduler
Loaded: loaded (/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
Active: active (running) since <date_time>; <time_ago>
Docs: man:atd(8)
Main PID: <pid> (atd)
Tasks: 1 (limit: 4622)
Memory: 248.0K
CPU: 11ms
CGroup: /system.slice/atd.service
└─<pid> /usr/sbin/atd -f
If the service is not active, you can start it with the following command:
sudo systemctl start atd
Finally, let's verify that the cron service is also active, as we will use it in a later step.
sudo systemctl status cron
The output should also show active (running), confirming that the cron daemon is ready to handle scheduled tasks.
● cron.service - Regular background program processing daemon
Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled)
Active: active (running) since <date_time>; <time_ago>
Docs: man:cron(8)
Main PID: <pid> (cron)
Tasks: 1 (limit: 4622)
Memory: 1.1M
CPU: 40ms
CGroup: /system.slice/cron.service
└─<pid> /usr/sbin/cron -f
With both atd and cron services running, your environment is now fully prepared for scheduling processes.
Schedule a One-Time Job with the at Command
In this step, you will learn how to use the at command to schedule a task to be executed once at a future time. This is useful for running scripts or commands that you don't need to execute immediately but also don't need to repeat on a regular schedule. The at command reads a series of commands from standard input and collects them into a single "at-job" that will be executed at a specified time.
Let's schedule a simple command that will create a file named at_output.txt in your current project directory (~/project). We'll set it to run one minute from now.
First, invoke the at command with the desired time specification.
at now + 1 minute
After you press Enter, your terminal prompt will change to at>. This indicates that at is waiting for you to input the commands you want to schedule. Now, type the command to create the file and write some text into it.
echo "The at job executed successfully." > ~/project/at_output.txt
Press Enter after typing the command. To finalize the job and exit the at> prompt, press Ctrl-D (hold the Ctrl key and press D). You will see a confirmation message that the job has been scheduled, along with its job number and execution time.
warning: commands will be executed using /bin/sh
job 1 at Mon Jan 1 12:01:00 2024
Now, you need to wait for one minute for the scheduled job to run.
After a minute has passed, you can verify that the job was executed by checking for the existence of the at_output.txt file. Use the ls -l command to see the file's details.
ls -l ~/project/at_output.txt
If the job ran successfully, the file will be listed in the output.
-rw-r--r-- 1 labex labex 35 Jan 1 12:01 /home/labex/project/at_output.txt
Next, view the contents of the file using the cat command to ensure the echo command worked as expected.
cat ~/project/at_output.txt
You should see the following output in your terminal:
The at job executed successfully.
Finally, to keep your project directory clean for the next steps, remove the file you just created.
rm ~/project/at_output.txt
You have now successfully scheduled, verified, and cleaned up a one-time job using the at command.
Manage Pending Jobs with atq and atrm
In this step, you will learn how to view and cancel scheduled at jobs. It's common to schedule a task and later realize it's no longer needed or needs to be modified. The atq command allows you to see the queue of pending jobs, and the atrm command lets you remove them.
First, let's schedule a job far enough in the future that we have time to manage it. We'll schedule a command to run in 10 minutes.
at now + 10 minutes
At the at> prompt, enter a command to create a file. We'll name the file temporary_job.txt.
touch ~/project/temporary_job.txt
Press Enter, then press Ctrl-D to save the job. The system will confirm the job creation and display its job number. Make a note of this job number, as you will need it to remove the job.
warning: commands will be executed using /bin/sh
job 2 at Mon Jan 1 12:10:00 2024
(Note: Your job number and time will be different.)
Now, to see the list of all your pending jobs, use the atq (at queue) command.
atq
The output will list your scheduled job, showing its job number, the date and time it's scheduled to run, the queue it's in (a), and the user who scheduled it.
2 Mon Jan 1 12:10:00 2024 a labex
Suppose you've decided you no longer want this job to run. You can cancel it using the atrm (at remove) command, followed by the job number you noted earlier.
Replace <job_number> in the command below with your actual job number. For example, if your job number was 2, you would run atrm 2.
atrm <job_number>
The command will not produce any output if it successfully removes the job.
To verify that the job has been removed from the queue, run atq again.
atq
This time, the command should produce no output. This silence confirms that your at job queue is empty and the temporary_job.txt file will not be created.
Create a Recurring Job with crontab -e
In this step, you will learn how to schedule a recurring task using cron. Unlike at, which runs a job once, cron is designed to run jobs repeatedly on a schedule. You'll manage your scheduled jobs in a special file called a crontab.
To edit your user's crontab file, you use the crontab -e command. The -e stands for "edit".
Let's open the crontab file for editing.
crontab -e
If this is the first time you are running crontab -e, you may be prompted to choose a default text editor. We recommend nano because it is easy to use.
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
...
Choose 1-2 [1]:
Press 1 and then Enter to select nano. The crontab file will open. It will be mostly empty, except for some comments explaining how to use it.
A crontab entry has a specific format with six fields:
minute hour day-of-month month day-of-week command
An asterisk (*) in a time field acts as a wildcard, meaning "every". For our task, we want to run a command every minute. This is perfect for testing because we don't have to wait long to see the result. The schedule for "every minute" is * * * * *.
Now, add a new line at the end of the file to schedule a job that appends the current date and time to a log file named cron_log.txt in your project directory.
* * * * * date >> ~/project/cron_log.txt
After adding the line, your editor should look something like this:
## Edit this file to introduce tasks to be run by cron.
#
## Each task to run has to be defined through a single line
## indicating with different fields when the task will be run
## and what command to run for the task
#
## To define the time you can provide concrete values for
## minute (m), hour (h), day of month (dom), month (mon),
## and day of week (dow) or use '*' in these fields (for 'any').
#
## Notice that tasks will be started based on the cron's system
## daemon's notion of time and timezones.
#
## Output of the crontab jobs (including errors) is sent through
## email to the user the crontab file belongs to (unless redirected).
#
## For example, you can run a backup of all your user accounts
## at 5 a.m. every week with:
## 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
## For more information see the manual pages of crontab(5) and cron(8)
#
## m h dom mon dow command
* * * * * date >> ~/project/cron_log.txt
To save the file and exit nano, press Ctrl-X, then press Y to confirm the changes, and finally press Enter to write to the file.
Once you exit, you will see a confirmation message in your terminal:
crontab: installing new crontab
This means your new cron job is active. The cron daemon will now check this file every minute and execute your command.
Wait for at least one minute. Then, verify that the log file has been created.
ls -l ~/project/cron_log.txt
You should see the file listed.
-rw-r--r-- 1 labex labex 29 Jan 1 12:15 /home/labex/project/cron_log.txt
Now, view its contents.
cat ~/project/cron_log.txt
The output will show the date and time when the command was first executed.
Mon Jan 1 12:15:01 UTC 2024
If you wait another minute and run the cat command again, you will see a new line with an updated timestamp, demonstrating that the job is running repeatedly.
Verify and Remove a Cron Job with crontab -l and -r
In this final step, you will learn how to view your scheduled cron jobs without opening the editor and how to remove them entirely. These are essential management tasks for keeping your system's scheduled tasks organized and clean.
First, let's list the currently active cron jobs. The crontab -l command (where -l stands for "list") displays the content of your crontab file directly to the terminal.
crontab -l
The output will show the job you created in the previous step.
## Edit this file to introduce tasks to be run by cron.
## ... (other comments) ...
## m h dom mon dow command
* * * * * date >> ~/project/cron_log.txt
This command is a safe, read-only way to check what jobs are scheduled.
Now, let's assume you no longer need this recurring job. To remove it, you can use the crontab -r command (where -r stands for "remove"). Be very careful with this command, as it removes the entire crontab file for your user without asking for confirmation.
Execute the command to remove your crontab.
crontab -r
The command will not produce any output if it is successful.
To verify that your crontab has been deleted, run the list command again.
crontab -l
This time, the command will return an error message indicating that you no longer have a crontab file.
no crontab for labex
This confirms that your scheduled job has been successfully removed and will no longer run.
Finally, to complete the lab and clean up your workspace, remove the cron_log.txt file that was created by the cron job.
rm ~/project/cron_log.txt
You have now successfully listed, removed, and verified the removal of a cron job, completing the full management lifecycle.
Summary
In this lab, you learned how to prepare a Linux environment for task scheduling by installing the at package and verifying that the atd and cron services were active. You practiced scheduling a one-time job for future execution using the at command. You also learned how to manage these pending jobs by viewing the queue with atq and removing a specific job with atrm.
Furthermore, you explored how to schedule recurring tasks using cron. You used the crontab -e command to edit the user's crontab file and add a new automated job. Finally, you learned how to verify the list of currently scheduled cron jobs with crontab -l and how to remove all of a user's scheduled cron jobs using the crontab -r command.



