Schedule tasks using cron, at, and systemd timer units

7. Deploy, Configure, and Maintain Systems

πŸ“˜Red Hat Certified System Administrator (RHCSA – EX200)


In Linux, automating tasks is essential for system administration. You rarely want to do everything manually, so Linux provides tools to schedule jobs to run automatically. The main tools for this are:

  1. cron – for recurring jobs
  2. at – for one-time jobs
  3. systemd timer units – newer method, works with systemd, both recurring and one-time jobs

1. Using cron

cron is a service that runs in the background (daemon) and executes commands at scheduled intervals.

How it works

  • cron reads crontab files which list commands to run and when to run them.
  • Each user can have their own crontab, including root.

Crontab File Format

Each line in a crontab file follows this pattern:

* * * * * command
- - - - -
| | | | |
| | | | +---- Day of week (0-7) (Sunday=0 or 7)
| | | +------ Month (1-12)
| | +-------- Day of month (1-31)
| +---------- Hour (0-23)
+------------ Minute (0-59)

Example: Run a backup script every day at 2:30 AM:

30 2 * * * /usr/local/bin/backup.sh

Commands to Manage crontab

  • crontab -e β†’ edit your own crontab
  • crontab -l β†’ list your crontab
  • crontab -r β†’ remove your crontab

Special Strings in crontab: These make scheduling easier:

  • @reboot β†’ run once at boot
  • @daily β†’ run once per day
  • @weekly β†’ run once per week
  • @monthly β†’ run once per month
  • @hourly β†’ run once per hour

Example:

@daily /usr/local/bin/cleanup_logs.sh

System-wide crontab

  • Located in /etc/crontab
  • Also, scripts can be placed in /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, /etc/cron.monthly for automatic execution.

2. Using at

at is for scheduling one-time jobs, unlike cron which is recurring.

How to use at

  1. Install at if not installed:
sudo yum install at
  1. Start the service (if not running):
sudo systemctl enable --now atd
  1. Schedule a command:
echo "/usr/local/bin/script.sh" | at 14:30

or interactively:

at 14:30
at> /usr/local/bin/script.sh
at> <Ctrl+D>
  1. List pending jobs:
atq
  1. Remove a scheduled job:
atrm <job_number>

Time formats supported:

  • HH:MM (24-hour format)
  • now + 10 minutes
  • tomorrow
  • next week

Example: Run a patch script at 3 PM today:

at 15:00
at> /usr/local/bin/apply_patches.sh
at> <Ctrl+D>

3. Using systemd Timer Units

Modern Linux uses systemd, which can replace cron and at for both recurring and one-time jobs. This is becoming preferred in Red Hat systems.

Components

  1. Service unit (.service) – defines what to run
  2. Timer unit (.timer) – defines when to run it

Example Setup

  1. Create a service file: /etc/systemd/system/backup.service
[Unit]
Description=Daily Backup Service[Service]
Type=oneshot
ExecStart=/usr/local/bin/backup.sh
  1. Create a timer file: /etc/systemd/system/backup.timer
[Unit]
Description=Run backup daily at 2:30 AM[Timer]
OnCalendar=*-*-* 02:30:00
Persistent=true[Install]
WantedBy=timers.target
  1. Enable and start the timer:
sudo systemctl enable --now backup.timer
  1. Check timer status:
systemctl list-timers

OnCalendar values:

  • *-*-* 02:30:00 β†’ daily at 2:30 AM
  • Mon *-*-* 12:00:00 β†’ every Monday at noon

Advantages over cron/at:

  • Integrated with systemd logs (journalctl)
  • Can trigger based on events, not just time
  • Better error handling

4. Practical IT Examples

  • Cron: Schedule log rotation every night at 1 AM:
0 1 * * * /usr/sbin/logrotate /etc/logrotate.conf
  • At: Schedule a server restart at 11 PM for maintenance:
echo "shutdown -r now" | at 23:00
  • Systemd Timer: Automatically run a backup script every day at 2 AM and log output:
/etc/systemd/system/backup.service
/etc/systemd/system/backup.timer

5. Key Exam Points

  1. Understand differences:
    • cron β†’ recurring tasks
    • at β†’ one-time tasks
    • systemd timer β†’ modern replacement, more flexible
  2. Know commands:
    • crontab -e/-l/-r, at, atq, atrm, systemctl enable/start/status timer.service
  3. Crontab syntax – must know the 5 fields and special strings.
  4. Systemd timer units – create .service + .timer, enable/start, check timers.
  5. Always ensure services are running:
systemctl status crond
systemctl status atd
systemctl status backup.timer

βœ… This covers everything for RHCSA 7.1 – scheduling tasks. If you master these commands, understand the syntax, and can create cron/at/systemd timers, you’ll pass this part of the exam.

Buy Me a Coffee