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
Open your
crontabfor editing:crontab -eAdd this line:
*/5 * * * * echo "Hello, world! The time is $(date)" >> ~/cron_test.logThis task will add a line with the current time to the
cron_test.logfile every 5 minutes. Save the file and exit.Check if the task started:
tail -f ~/cron_test.logAfter 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:
Write a script:
#!/bin/bash tar -czf /backup/logs_$(date +\%Y-\%m-\%d).tar.gz /var/logsSet 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%:
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 fiSet 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
Create a task for a notification in 2 minutes:
echo "notify-send 'Time is up!'" | at now + 2 minutesCheck the list of tasks:
atqWait 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
cronand 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!
GO TO FULL VERSION