Automating repetitive tasks is essential for system administration and productivity. Linux provides powerful tools like cron
, at
, and systemd
timers to schedule and manage tasks efficiently. This chapter explores these tools in detail and demonstrates how to use them effectively.
- Job: A task or script that is scheduled to run automatically.
- Crontab: A configuration file for scheduling recurring tasks.
- One-time jobs: Tasks that are scheduled to run only once using tools like
at
.
- A daemon that executes scheduled tasks defined in
crontab
files.
Each line in a crontab
file represents a scheduled task:
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +----- Day of the week (0 - 7, 0 or 7 = Sunday)
| | | +------- Month (1 - 12)
| | +--------- Day of the month (1 - 31)
| +----------- Hour (0 - 23)
+------------- Minute (0 - 59)
- View the current crontab:
crontab -l
- Edit the crontab:
crontab -e
- Remove the current crontab:
crontab -r
- Run a script every day at midnight:
0 0 * * * /path/to/script.sh
- Execute a command every Monday at 9 AM:
0 9 * * 1 /path/to/command
- Clear the system cache every hour:
0 * * * * sudo sync; sudo echo 3 > /proc/sys/vm/drop_caches
- A command-line utility for scheduling one-time tasks.
at time
- Schedule a task to run at 5 PM:
echo "backup.sh" | at 17:00
- Schedule a task to run in 2 hours:
echo "reboot" | at now + 2 hours
- List all scheduled jobs:
atq
- Delete a job by its ID:
atrm <job_id>
- A modern alternative to
cron
, offering more flexibility and integration withsystemd
.
-
Create a service file (e.g.,
/etc/systemd/system/my-task.service
):[Unit] Description=My Scheduled Task [Service] ExecStart=/path/to/script.sh
-
Create a timer file (e.g.,
/etc/systemd/system/my-task.timer
):[Unit] Description=Runs My Scheduled Task [Timer] OnCalendar=*-*-* 00:00:00 [Install] WantedBy=timers.target
-
Enable and start the timer:
sudo systemctl enable my-task.timer sudo systemctl start my-task.timer
-
Check the timer’s status:
sudo systemctl list-timers
- Permissions: Ensure the user running the task has the necessary permissions.
- Environment Variables:
cron
uses a minimal environment. Define paths explicitly in your scripts. - Logs: Check logs for errors.
- For
cron
:grep CRON /var/log/syslog
- For
systemd
timers:journalctl -u my-task.service
- For
By the end of this chapter, you should be able to:
- Schedule recurring tasks using
cron
. - Set up one-time jobs with
at
. - Use
systemd
timers for advanced scheduling. - Debug and troubleshoot scheduled jobs effectively.
- Move to Chapter 17: Python Scripting Basics for Hackers to explore scripting in Python for automation and hacking.
- Schedule a script to run every day at 3 AM using
cron
. - Create a one-time job using
at
to shut down the system in 1 hour. - Set up a
systemd
timer to run a task every Monday at 6 PM. - Check the logs of a failed
cron
job and debug the issue. - Compare the output of
systemctl list-timers
withcron
to understand the differences in scheduling.