πŸ•’ Linux/Unix: Job Scheduling – cron, crontab, at, batch Commands Explained

🧲 Introduction – Why Learn Job Scheduling in Linux?

Automating tasks is crucial for efficient system administration. Whether you’re running backups at night, sending email alerts, or rotating logs, Linux job scheduling tools like cron, crontab, at, and batch let you schedule commands and scripts to run periodically or once at a specific timeβ€”no manual intervention needed.

🎯 In this guide, you’ll learn:

  • How to schedule recurring jobs using cron/crontab
  • How to run one-time tasks using at and batch
  • Real command usage, examples, and common scheduling formats

πŸ” 1. cron & crontab – Repeating Jobs Scheduler

βœ… What is cron?

cron is a time-based job scheduler that runs scripts or commands at fixed intervals (minutes, hours, days, etc.).

βœ… What is crontab?

crontab is the table of cron jobs, storing user-defined scheduled tasks.


πŸ› οΈ Crontab Syntax Format:

*  *  *  *  *  command-to-run
β”‚  β”‚  β”‚  β”‚  β”‚
β”‚  β”‚  β”‚  β”‚  └─ Day of week (0-7; 0 or 7 = Sunday)
β”‚  β”‚  β”‚  └──── Month (1-12)
β”‚  β”‚  └─────── Day of month (1-31)
β”‚  └────────── Hour (0-23)
└───────────── Minute (0-59)

πŸ§ͺ Example: Run backup script daily at 2 AM

crontab -e

Add:

0 2 * * * /home/user/scripts/backup.sh

πŸ”Ή Common Crontab Commands:

CommandDescription
crontab -eEdit user’s crontab file
crontab -lList current cron jobs
crontab -rRemove user’s crontab

πŸ§ͺ Example: Run every 5 minutes

*/5 * * * * /usr/bin/python3 /home/user/check.py

🧠 Logs can be found in:

/var/log/syslog  (Debian/Ubuntu)

or

/var/log/cron    (RHEL/CentOS)

πŸ• 2. at – One-Time Job Scheduler

βœ… What is at?

The at command runs a command or script once at a specific time in the future.

πŸ› οΈ Install it (if not present):

sudo apt install at   # Debian/Ubuntu
sudo yum install at   # RHEL/CentOS

Enable atd daemon:

sudo systemctl start atd
sudo systemctl enable atd

πŸ§ͺ Example: Run script at 10:00 PM today

echo "/home/user/cleanup.sh" | at 10:00 PM

πŸ§ͺ Example: Run at 2 AM tomorrow

at 2am tomorrow

Inside prompt:

/home/user/backup.sh
<Ctrl+D>

πŸ”Ή Other at Commands:

CommandDescription
atqList scheduled jobs
atrm <job#>Remove a scheduled job

πŸ“‰ 3. batch – Run When System Load Is Low

βœ… What is batch?

batch is like at, but it waits until system load is low to execute tasks. Ideal for non-urgent tasks.


πŸ§ͺ Example:

echo "/home/user/reports.sh" | batch

πŸ“€ Output:

job 6 at Tue Jun 18 21:00:00 2025

🧠 Uses the same backend as at.


βš™οΈ Tool Comparison Table

ToolUse CaseFrequencyInteractive?System Load Aware
cronRecurring tasks (daily/hourly)RepeatedNoNo
crontabDefine user job scheduleRepeatedNoNo
atOne-time future jobsOnceYes (prompt)No
batchOne-time jobs on low CPUOnceYesβœ… Yes

πŸ“Œ Summary – Recap & Next Steps

Linux scheduling tools empower you to automate everything from backups to reports. With cron and crontab, you handle repetitive tasks. Use at or batch for ad-hoc or load-aware jobs, especially useful in scripting and DevOps pipelines.

πŸ” Key Takeaways:

  • Use crontab for scheduled recurring jobs.
  • Use at for a one-time future execution.
  • Use batch to run when system is idle.
  • Use atq, atrm, and crontab -l for job management.

❓ FAQs

❓ How do I know if cron is running?
βœ… Run:

sudo systemctl status cron     # Ubuntu/Debian
sudo systemctl status crond    # RHEL/CentOS

❓ Where are crontab logs stored?
βœ… Usually in:

/var/log/syslog

or

/var/log/cron

❓ Can I run GUI apps using cron?
🟑 Not recommended. GUI apps need environment variables like DISPLAY.

❓ How do I run a job every Sunday?
βœ… Use:

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

❓ What’s the difference between cron and at?
βœ… cron runs jobs repeatedly. at runs a job only once.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: Job Scheduling (cron, crontab, at, batch)

Or Copy Link

CONTENTS
Scroll to Top