Automating Tasks in Linux with at and crontab

Scheduling One-Time and Recurring Tasks

Linux provides mechanisms to schedule tasks for future execution. One-time jobs run at a specific moment, while recurring jobs repeat at defined intervals. The primary tools for these operations are at (for one-time execution) and crontab (for recurring execution).

Local Mail Service

Task execution results are typical sent to the user via email. To verify if the mail service is listening, check for the SMTP port:

ss -tnl | grep :25

To send emails manually from the terminal, install the necessary utilities:

yum -y install sendmail mailx

Sending a message to a user named operator:

mail -s 'System Update Notification' operator <<< 'The update process has completed.'

Or using a file as the body:

mail -s 'Disk Report' operator < /tmp/disk_report.txt

One-Time Scheduling with at

The atd daemon must be running to execute deferred tasks.

Installation and Service Check:

yum -y install at
systemctl status atd

Basic Usage:

at [options] TIME

Common flags:

  • -l: List pending jobs (equivalent to atq).
  • -d: Delete a job (equivalent to atrm).
  • -f: Read commands from a file instead of standard input.
  • -c: Display the content of a specific job.

Time Specification Examples:

  • at 02:00: Runs today at 2 AM or tomorrow if the time has passed.
  • at 04:00 2023-11-20: Runs at 4 AM on a specific date.
  • at now + 5 minutes: Runs 5 minutes from the currrent time.
  • at teatime tomorrow: Runs at 4 PM the next day.

Example Workflow:

  1. Schedule a script to run in one minute:
    at now + 1min
    at> /home/user/cleanup.sh
    at> <EOT> (Press Ctrl+D)
    
  2. Check the queue:
    at -l
    

Recurring Scheduling with cron

The cron system handles periodic tasks. Ensure the daemon is active:

systemctl status crond

System Cron (/etc/crontab)

System-level tasks are defined in /etc/crontab. This file requires a user field to specify the identity running the command.

Format:

* * * * * user command
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └───── Day of the week (0 - 7) (Sunday=0 or 7)
│ │ │ └────────── Month (1 - 12)
│ │ └─────────────── Day of the month (1 - 31)
│ └──────────────────── Hour (0 - 23)
└───────────────────────── Minute (0 - 59)

Example: Run a script every hour at minute 1 as root.

1 * * * * root /usr/local/bin/hourly_check.sh

User Cron (crontab)

Users manage their personal cron jobs using the crontab command. This does not require the 'user' field.

Managing Jobs:

  • crontab -e: Edit the current user's cron table.
  • crontab -l: List current jobs.
  • crontab -r: Remove all jobs.
  • crontab -u username -l: List jobs for a specific user (requires root).

Time Syntax Examples:

  1. Specific Values: 30 4 1 * * (Runs at 4:30 AM on the 1st of every month).
  2. Wildcards (*): 0 * * * * (Runs every hour at minute 0).
  3. Lists (,): 0 8,12,16 * * * (Runs at 8 AM, 12 PM, and 4 PM).
  4. Ranges (-): 0 9-17 * * 1-5 (Runs every hour from 9 AM to 5 PM, Monday through Friday).
  5. Step Values (/): */10 * * * * (Runs every 10 minutes).

Special Strings:

  • @reboot: Run once at startup.
  • @daily: Run once a day (equivalent to 0 0 * * *).
  • @weekly: Run once a week (equivalent to 0 0 * * 0).

Note on Output: To prevent mail notifications or clutter, redirect output to null or a log file:

0 * * * * /path/to/script.sh > /dev/null 2>&1

Practical Examples

  1. Backup /etc every 4 hours:

    0 */4 * * * /bin/tar -czf /backup/etc-$(date +\%F-\%H).tar.gz /etc
    
  2. Backup logs on Tuesdays, Thursdays, and Sundays:

    20 1 * * 2,4,0 /bin/cp /var/log/messages /logs/messages-$(date +\%Y\%m\%d)
    
  3. Monitor memory info every 2 hours:

    20 */2 * * * /bin/grep -E '^(S|M)' /proc/meminfo >> /tmp/meminfo.txt
    
  4. Show IP address during work hours (every 2 hours from 9 to 17):

    0 9-17/2 * * 1-5 /sbin/ip addr show
    

Tags: Linux cron Task Scheduling automation System Administration

Posted on Thu, 22 Oct 2026 16:52:29 +0000 by iovidiu