CodeGym /Courses /Docker SELF /Task Automation with cron and at

Task Automation with cron and at

Docker SELF
Level 3 , Lesson 5
Available

Task Automation with cron and at

1. Why do we need automation?

Task automation is the foundation for any sysadmin, especially when it comes to Linux. Imagine working without automation: every morning, you open the terminal and type the same command to make backups of your data, archive them, send them to a server, and clean up old logs. After a week, this routine will make you question your career choice… But no, we’re here to teach you how to make your machine your loyal servant.

Today's lecture is all about the magic of time and tasks. We’ll learn how to automate processes on an operating system level using the incredible tools cron and at. These utilities save a ton of time, and sometimes even your sanity.

Automation is a lifesaver for any routine task. For example, imagine you’ve got a script that generates a report for your boss. This report is needed every morning at 8:00 AM. Instead of waking up earlier to manually run the script (and risk getting stuck with your mug of coffee and forgetting), you can set up cron to handle this task automatically.

cron is a tool for regular tasks that repeat at specified times.
at is a utility for one-time tasks that need to be executed at a specific moment.


2. Working with cron

What is cron?

cron is a daemon (a system process) that runs in the background and executes tasks scheduled at specified times. The schedules for these tasks are stored in what’s known as crontab — text files. Every user on the system can have their own crontab.

Your first task in cron

Here’s what a basic command for working with cron looks like:

crontab -e

This command opens the current user’s crontab for editing. You can add tasks by specifying the schedule format.

Schedule Syntax

A line in crontab consists of six fields:

* * * * * COMMAND
- - - - -
| | | | └─ Day of the week (0–7, where 0 and 7 are Sunday)
| | | └── Month (1–12)
| | └─── Day of the month (1–31)
| └──── Hour (0–23)
└───── Minute (0–59)

Example: execute echo "Hello, Linux" every day at noon:

0 12 * * * echo "Hello, Linux"

Practice: Run a simple task

  1. Open your crontab for editing:

    crontab -e
    
  2. Add this line:

    */5 * * * * echo "Hello, world! The time is $(date)" >> ~/cron_test.log
    

    This task will add a line with the current time to the cron_test.log file every 5 minutes. Save the file and exit.

  3. Check if the task started:

    tail -f ~/cron_test.log
    

    After 5 minutes, you should see an updated entry in the file.

Viewing Scheduled Tasks

To check the current tasks:

crontab -l

Deleting Tasks

To delete all tasks from crontab:

crontab -r

Real Use Cases for cron

File Archiving

Let’s say you want to archive files from the /var/logs directory every night at 3 AM:

  1. Write a script:

    #!/bin/bash
    tar -czf /backup/logs_$(date +\%Y-\%m-\%d).tar.gz /var/logs
  2. Set up the schedule in crontab:

    0 3 * * * /path/to/script.sh
    

And that’s it! You’ll never forget to back up again.

System Load Notification

For example, you want to get notified if the CPU load is above 80%:

  1. Write a script:

    #!/bin/bash
    LOAD=$(uptime | awk '{print $10}' | sed 's/,//')
    if (( $(echo "$LOAD > 0.80" | bc -l) )); then
        echo "High CPU load: $LOAD" | mail -s "CPU Alert" you@example.com
    fi
  2. Set up the schedule:

    */10 * * * * /path/to/cpu_check.sh
    

3. Working with at

at lets you execute a one-time task at a specific time. Unlike cron, at tasks do not repeat. It’s handy for tasks like "do this tomorrow at 9 am".

Simple task with at

Here’s an example of how to schedule a task:

echo "echo 'Hello, Linux!'" | at now + 1 minute

This command will execute echo 'Hello, Linux!' in one minute.

You can specify an exact time and date:

echo "echo 'Backup completed!'" | at 10:30 AM tomorrow

Managing at tasks

After adding a task, you can view the list of tasks:

atq

Example output:

1   Tue Oct 31 10:30:00 2023 a user
2   Tue Oct 31 11:00:00 2023 a user

To remove a task:

atrm <job_id>

Practice: using at for notifications

  1. Create a task for a notification in 2 minutes:

    echo "notify-send 'Time is up!'" | at now + 2 minutes
    
  2. Check the list of tasks:

    atq
    
  3. Wait for the pop-up notification in 2 minutes.


4. Common mistakes when working with cron and at

One of the most frequent issues is specifying an incorrect path to the script or command. Remember, cron runs in a minimal environment, so it might not know about your variables, like PATH. The best approach is to use absolute paths.

For example, if you want to run a Python script:

*/30 * * * * /usr/bin/python3 /path/to/script.py

Also, make sure the script has execution rights:

chmod +x /path/to/script.sh

Where can this be useful?

Automation skills are required in almost every IT-related profession. You can use this knowledge:

  • To automate routine system tasks.
  • To set up periodic backups.
  • To monitor server health.
  • In job interviews, where questions about cron and automation frequently come up.

If you work in DevOps, system administration, or just want to be more productive, understanding cron and at will help you stand out and save a ton of time.

Now you know how to make Linux work for you. Next time someone complains about routines, just smile because you're the automation master!

1
Task
Docker SELF, level 3, lesson 5
Locked
Working with `crontab` for scheduled tasks
Working with `crontab` for scheduled tasks
1
Task
Docker SELF, level 3, lesson 5
Locked
Using `at` for one-time tasks
Using `at` for one-time tasks
1
Task
Docker SELF, level 3, lesson 5
Locked
Archiving directories using `cron`
Archiving directories using `cron`
1
Task
Docker SELF, level 3, lesson 5
Locked
Automatic CPU Load Notification
Automatic CPU Load Notification
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION