- Posted on
- • commands
Scheduling Tasks with `cron` and `at`
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
Mastering Time: Scheduling Tasks with cron
and at
in Linux
For anyone managing servers or maintaining a system, automating routine tasks is essential. Not only does automation save time, but it also eliminates the possibility of human error in repetitive tasks. Linux, known for its robustness and flexibility, offers powerful tools for automating tasks: cron
and at
. These tools are indispensable for system administrators and savvy users alike. Today, we’ll explore how to use these tools effectively to schedule tasks and make your sysadmin life a little easier.
Understanding cron
The cron
daemon is one of the most useful utilities in a Linux environment. It allows tasks to be automatically performed at specified intervals. Each task scheduled by cron
is called a "cron job."
Configuring Cron Jobs: Cron jobs are defined in a crontab, which is a configuration file that specifies shell commands to run periodically on a given schedule. Each user on a system can have their own crontab, and there’s also a system-wide crontab.
To edit your personal crontab file, run:
crontab -e
Each line in a crontab file consists of six fields: minute, hour, day of the month, month, day of the week, and the command to execute, in that order. For example:
0 5 * * * /usr/bin/python3 /home/user/backup.py
This job triggers at 5:00 AM every day. The asterisks are wildcards that stand for "every."
Special Syntax:
Cron also supports special strings such as @daily
, @hourly
, and @reboot
, which simplifies scheduling:
@daily /usr/bin/python3 /home/user/backup.py
This line achieves the same as the earlier example, running a Python script every day.
Navigating at
While cron
is perfect for recurring jobs, the at
command is designed for one-time tasks. It runs a job at a certain time and then the job is removed from the queue.
Using at
:
First, you might need to install at
, as it may not be included by default. In Ubuntu, you can install it by running:
sudo apt-get install at
To schedule a job with at
, type at
followed by the time the command should run:
at 22:45
After pressing Enter, you'll be prompted to enter the commands you want to execute, followed by pressing Ctrl+D
to end.
We can also specify absolute times like:
at 8am July 11
at now + 2 days
The jobs are executed in a non-interactive shell, and the output is mailed to the user.
Viewing Scheduled Jobs:
To see your scheduled at
jobs, use:
atq
#### Comparisons and When to Use Each
Use
cron
for recurring tasks, such as daily database backups, weekly reports, or hourly syncs.Use
at
for one-off tasks like shutting down a server at 10 PM, or running a script next Friday at noon.
Conclusion
Automating routine tasks in a Linux environment can save tremendous time and increase productivity. With cron
and at
, Linux provides you the tools to schedule nearly any task for any point in the future, thereby making repetitive schedules or one-time events something not to worry about. Whether you are a seasoned system administrator or a casual Linux user, mastering these commands can help you take full control of your tasks and your time.