DEV Community

LaTerral Williams
LaTerral Williams

Posted on

2

๐Ÿ•ฐ๏ธBecome a Timecop with Linux Task Scheduling: at, cron, and crontab

Imagine if you had the power to schedule events in time like Max Walker from Timecop. Only instead of fighting crime in 1994, you're automating tasks in Linux.

In this article, we'll review how to:

  • Set up a Linux environment for practice

  • Use at for one-time tasks

  • Use cron and crontab for recurring tasks

  • Work with system-level scheduling using /etc/cron.*

  • Write and schedule your own scripts

  • Investigate and manage scheduled tasks

Letโ€™s hop into our time machine (terminal) and begin.

Image description


๐Ÿ“š Table of Contents


๐Ÿ–ฅ๏ธ Setting Up Your Timecop Training Ground (optional)

๐Ÿงช Step 1: Install RHEL 9

  • Download RHEL 9
  • Use VirtualBox, VMware, or install on a spare machine
  • Register the system (for updates):
  sudo subscription-manager register
  sudo subscription-manager attach --auto
  sudo dnf update -y
Enter fullscreen mode Exit fullscreen mode

โฐ Scene 1: Scheduling a One-Time Mission with at

๐Ÿง  The Mission:

Max needs to send a system alert exactly 2 minutes from now.

โœ… Step 1: Install and enable at service

sudo dnf install at -y
sudo systemctl enable --now atd
Enter fullscreen mode Exit fullscreen mode

โœ… Step 2: Schedule a task

echo "Timecop alert at $(date)" >> /tmp/timecop_alert.log | at now + 2 minutes
Enter fullscreen mode Exit fullscreen mode

โœ… Step 3: View and manage at jobs

atq         # View queue
atrm <job>  # Remove job
Enter fullscreen mode Exit fullscreen mode

Image description

Image description


โ™ป๏ธ Scene 2: Scheduling Recurring Missions with cron

๐Ÿง  The Mission:

Max must run a time anomaly scan every minute.

โœ… Step 1: Create a script

mkdir -p ~/timecop/scripts
vim ~/timecop/scripts/scan.sh
Enter fullscreen mode Exit fullscreen mode
#!/bin/bash
echo "Anomaly scan run at $(date)" >> /tmp/timecop_scan.log
Enter fullscreen mode Exit fullscreen mode

โœ… Step 2: Make the script executable

chmod +x ~/timecop/scripts/scan.sh
Enter fullscreen mode Exit fullscreen mode

Image description

โœ… Step 3: Schedule with crontab

crontab -e
Enter fullscreen mode Exit fullscreen mode

Add this line:

* * * * * /home/yourusername/timecop/scripts/scan.sh
Enter fullscreen mode Exit fullscreen mode

I mage description

Feel free to set the schedule as you prefer. I set to run every minute in order to confirm the cronjob executes.

Image description

If you find your cronjob is not running. The cron service may not be installed and/or running. The steps to remedy this will vary based on the Linux distro you are using.

Here's a few examples for AlmaLinux:

sudo dnf install cronie
sudo systemctl start crond.service
sudo systemctl enable crond.service

๐Ÿงญ Diagram: Understanding Crontab Format

# โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Minute (0 - 59)
# โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Hour (0 - 23)
# โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Day of month (1 - 31)
# โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Month (1 - 12)
# โ”‚ โ”‚ โ”‚ โ”‚ โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Day of week (0 - 7) (Sunday = 0 or 7)
# โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
# โ”‚ โ”‚ โ”‚ โ”‚ โ”‚
# * * * * * command_to_execute
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Example:

15 14 1 * * /home/max/run_report.sh
Enter fullscreen mode Exit fullscreen mode

Runs the script at 2:15 PM on the 1st day of every month.


๐Ÿงฎ Diagram: Cron Schedule Examples

Schedule Crontab Expression Description
Every minute * * * * * Runs every minute
Every 5 minutes */5 * * * * Every 5 minutes
Every day at 1 AM 0 1 * * * Daily at 1:00 AM
Every Monday at noon 0 12 * * 1 Every Monday at 12:00 PM
First of every month 0 0 1 * * Midnight on the 1st of each month

๐Ÿ›๏ธ Scene 3: Using System-Level Missions with /etc/cron.*

RHEL has system directories that run scripts at fixed intervals.

/etc/cron.* folders:

  • /etc/cron.hourly/
  • /etc/cron.daily/
  • /etc/cron.weekly/
  • /etc/cron.monthly/

๐Ÿง  The Mission:

Create a system cleanup that runs weekly.

You will need to use sudo to direct changes related to /etc/cron.*

โœ… Step 1: Create a cleanup script

sudo vim /etc/cron.weekly/timecop_cleanup.sh
Enter fullscreen mode Exit fullscreen mode
#!/bin/bash
echo "System cleanup at $(date)" >> /var/log/timecop_cleanup.log
Enter fullscreen mode Exit fullscreen mode

โœ… Step 2: Set executable permission

sudo chmod +x /etc/cron.weekly/timecop_cleanup.sh
Enter fullscreen mode Exit fullscreen mode

Image description


๐Ÿ”Ž Scene 4: Investigating Scheduled Missions

โœ… View user's cron jobs

crontab -l
Enter fullscreen mode Exit fullscreen mode

โœ… View system crontab

cat /etc/crontab
Enter fullscreen mode Exit fullscreen mode

โœ… View scripts in system cron folders

ls /etc/cron.daily/
ls /etc/cron.weekly/
Enter fullscreen mode Exit fullscreen mode

โœ… View logs (RHEL-specific)

sudo grep CRON /var/log/cron
Enter fullscreen mode Exit fullscreen mode

โœ… View at jobs

atq
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘ค Scene 5: User vs Root: Who Runs What?

๐Ÿง  Mission Clarification:

  • crontab -e runs as your user.
  • /etc/crontab allows you to specify a user explicitly.

Example /etc/crontab line:

30 2 * * * root /usr/local/bin/timecop_backup.sh
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ก Tip: Use root only when needed... donโ€™t disrupt the timeline with elevated permissions!


๐ŸŽฏ Scene 6: Testing Your Scripts

Letโ€™s simulate a full time jump.

โœ… Create a test script

vim ~/timecop/scripts/test_jump.sh
Enter fullscreen mode Exit fullscreen mode
#!/bin/bash
echo "Time jump occurred at $(date)" >> /tmp/timecop_jump.log
Enter fullscreen mode Exit fullscreen mode

โœ… Make it executable

chmod +x ~/timecop/scripts/test_jump.sh
Enter fullscreen mode Exit fullscreen mode

โœ… Run once with at

echo "/home/yourusername/timecop/scripts/test_jump.sh" | at now + 1 minute
Enter fullscreen mode Exit fullscreen mode

โœ… Schedule via crontab

crontab -e
# Add:
*/1 * * * * /home/yourusername/timecop/scripts/test_jump.sh
Enter fullscreen mode Exit fullscreen mode

Image description


๐Ÿงพ Timecop's Summary Table

Tool Purpose Use Case
at One-time task Run a job once in the future
cron + crontab Recurring tasks Daily/weekly backups, reports
/etc/cron.* System-wide scripts Maintenance jobs
atq, crontab -l, logs Investigation Check the timeline for jobs

๐Ÿ•น๏ธ Final Thoughts: Time Is on Your Side

With at, cron, and a few bash scripts, you're now equipped to become a Linux Timecop; defending your system from disorganization and delay.

You canโ€™t rewrite history, but with scheduling, you can plan for the future.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)