Scheduling Tasks with cron
and at
Scheduling Tasks with cron
and at
in Ubuntu
Ubuntu provides two main tools for scheduling tasks: cron
and at
. These tools allow users to automate scripts and commands, ensuring they are executed at specific times.
1. Using cron
cron
is a daemon that runs scheduled tasks at specific intervals (e.g., daily, hourly). These tasks are defined in the crontab (cron table) file.
-
Crontab Syntax:
* * * * * command_to_execute | | | | | | | | | +----- Day of the week (0 - 7) (Sunday = 0 or 7) | | | +------- Month (1 - 12) | | +--------- Day of the month (1 - 31) | +----------- Hour (0 - 23) +------------- Minute (0 - 59)
-
Example: Run a script every day at 7:30 AM.
- Open the crontab file with the command:
crontab -e
- Add the following line:
30 7 * * * /path/to/your_script.sh
This entry tells cron to run the script at 7:30 AM every day.
- Open the crontab file with the command:
2. Using at
at
is used for one-time task scheduling. It allows you to specify a command to be executed at a specific time in the future.
- Example: Run a script at 5:00 PM today.
- First, ensure the
at
service is installed and running:sudo apt-get install at sudo systemctl start atd
- Schedule the task with
at
:echo "/path/to/your_script.sh" | at 17:00
- First, ensure the
Output Example
-
Crontab Output Example: Suppose you scheduled a cron job to run a backup script daily. The output might be logged in a file you specify in the script, like:
Backup started at Fri Aug 10 07:30:01 UTC 2024 Backup completed at Fri Aug 10 07:35:10 UTC 2024
-
at
Output Example: If you schedule a one-time task to check disk usage at 5:00 PM, you might get an email or see output in the terminal:Disk usage checked at Fri Aug 10 17:00:01 UTC 2024 /dev/sda1 30% used
Summary
cron
: Best for recurring tasks.at
: Best for one-time tasks.
Using cron
and at
, you can efficiently manage task scheduling on Ubuntu, automating repetitive or time-specific tasks easily.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.