Skip to content

Scheduled_tasks

This chapter covers scheduled task management in Linux using cron, at, and systemd timers.


Cron System Architecture
+------------------------------------------------------------------+
| |
| +---------------+ +---------------+ +---------------+ |
| | crontab | | cron | | /etc/cron.d | |
| | files |---->| daemon |---->| scripts | |
| +---------------+ +---------------+ +---------------+ |
| | | | |
| v v v |
| +---------------+ +---------------+ +---------------+ |
| | User crontabs| | cron jobs | | System cron | |
| | /var/spool/ | | execution | | jobs | |
| +---------------+ +---------------+ +---------------+ |
| |
+------------------------------------------------------------------+
Terminal window
# ┌───────────── minute (0 - 59)
# │ ┌───────────── hour (0 - 23)
# │ │ ┌───────────── day of month (1 - 31)
# │ │ │ ┌───────────── month (1 - 12)
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday=0)
# │ │ │ │ │
# * * * * * command
# Examples:
0 5 * * * # Run at 5:00 AM daily
0 0 * * 0 # Run at midnight on Sundays
30 4 1,15 * * # Run at 4:30 AM on 1st and 15th
0 8 * * 1-5 # Run at 8 AM on weekdays
*/15 * * * * # Run every 15 minutes
0 */2 * * * # Run every 2 hours
@reboot # Run at reboot
@yearly # Run once a year (0 0 1 1 *)
@monthly # Run once a month (0 0 1 * *)
@weekly # Run once a week (0 0 * * 0)
@daily # Run once a day (0 0 * * *)
@hourly # Run every hour (0 * * * *)
Terminal window
# Edit current user's crontab
crontab -e
# List current user's crontab
crontab -l
# Remove current user's crontab
crontab -r
# Edit specific user's crontab (root only)
crontab -u username -e
# List specific user's crontab
crontab -u username -l
Terminal window
# System cron directories
/etc/cron.d/ # System-wide cron jobs
/etc/cron.daily/ # Scripts run daily
/etc/cron.hourly/ # Scripts run hourly
/etc/cron.monthly/ # Scripts run monthly
/etc/cron.weekly/ # Scripts run weekly
# Example: /etc/cron.d/maintance
# Run maintenance script at 2 AM daily
0 2 * * * root /usr/local/bin/maintenance.sh

Terminal window
# Install at if needed
sudo pacman -S at
# Schedule a job to run at specific time
at 2:30 PM
at> /path/to/command
# Ctrl+D to save
# Schedule a job for specific date
at 2:30 PM + 2 days
at 14:30 2026-12-25
# List pending jobs
atq
# or
at -l
# Remove a job
atrm job_number
Terminal window
# Run a command in 1 hour
at now + 1 hour
# Run a command tomorrow at 10 AM
at 10:00 tomorrow
# Run a script at boot
at now + 1 minute -f /path/to/script.sh
# View job details
at -c job_number

/etc/systemd/system/mytimer.timer
[Unit]
Description=Run daily cleanup
[Timer]
OnCalendar=daily
Persistent=true
RandomizedDelaySec=1h
[Install]
WantedBy=timers.target
/etc/systemd/system/mytimer.service
[Unit]
Description=Daily cleanup service
[Service]
Type=oneshot
ExecStart=/usr/local/bin/cleanup.sh
Terminal window
# Start timer
sudo systemctl start mytimer.timer
# Enable timer (start on boot)
sudo systemctl enable mytimer.timer
# List active timers
systemctl list-timers
systemctl list-timers --all
# View timer status
systemctl status mytimer.timer
# View timer next run
systemctl list-timers --all | grep mytimer
Terminal window
# Run every hour
OnCalendar=hourly
# or
OnCalendar=*:0/1
# Run every 30 minutes
OnCalendar=*:0/30
# Run at specific time daily
OnCalendar=*-*-* 02:00:00
# Run on specific days
OnCalendar=Mon *-*-* 09:00:00
# Run weekly (Sunday at 3 AM)
OnCalendar=Sun *-*-* 03:00:00
# Run monthly (1st of month at midnight)
OnCalendar=*-*-01 00:00:00

/etc/anacrontab
# Install
sudo pacman -S cronie # Contains anacron
# format: delay job_identifier command
1 5 cron.daily run-parts /etc/cron.daily
7 25 cron.weekly run-parts /etc/cron.weekly
30 60 cron.monthly run-parts /etc/cron.monthly
# Run anacron manually
sudo anacron -d

Terminal window
# Redirect output to log file
0 5 * * * /path/to/script.sh >> /var/log/script.log 2>&1
# Send email on failure
0 5 * * * /path/to/script.sh || echo "Failed" | mail -s "Cron Failed" admin@example.com
Terminal window
# In crontab, set PATH
PATH=/usr/local/bin:/usr/bin:/bin
# Set HOME
HOME=/root
# Set SHELL
SHELL=/bin/bash
Terminal window
# Problem: Missing PATH in cron
# Solution: Define PATH at top of crontab
# Problem: Commands not found
# Solution: Use full paths: /usr/bin/command
# Problem: Scripts not executing
# Solution: Make script executable: chmod +x script.sh
# Problem: Environment differences
# Solution: Source profile in script: source /etc/profile

Terminal window
# Complex calendar expressions
OnCalendar=Mon..Fri 09:00:00 # Weekdays at 9 AM
OnCalendar=*-*-1..7 18:00:00 # First week of month at 6 PM
OnCalendar=*:0/15 # Every 15 minutes
OnCalendar=Sat,Sun 10:00 # Weekends at 10 AM
# Combined with delays
OnBootSec=5min # 5 minutes after boot
OnUnitActiveSec=1h # 1 hour after last activation
OnUnitInactiveSec=30m # 30 minutes after deactivation

In this chapter, you learned:

  • ✅ Cron syntax and management
  • ✅ at command for one-time tasks
  • ✅ systemd timers for modern scheduling
  • ✅ Anacron for systems not running 24/7
  • ✅ Best practices and troubleshooting

Chapter 21: Networking Fundamentals


Last Updated: February 2026