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 tasksUse
cron
andcrontab
for recurring tasksWork 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.
๐ Table of Contents
- ๐ฅ๏ธ Setting Up Your Timecop Training Ground
- โฐ Scene 1: Scheduling a One-Time Mission with
at
- โป๏ธ Scene 2: Scheduling Recurring Missions with
cron
- ๐งญ Diagram: Understanding Crontab Format
- ๐งฎ Diagram: Cron Schedule Examples
- ๐๏ธ Scene 3: Using System-Level Missions with
/etc/cron.*
- ๐ Scene 4: Investigating Scheduled Missions
- ๐ค Scene 5: User vs Root โ Who Runs What?
- ๐ฏ Scene 6: Testing Your Scripts
- ๐งพ Timecop's Summary Table
- ๐น๏ธ Final Thoughts: Time Is on Your Side
๐ฅ๏ธ 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
โฐ 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
โ Step 2: Schedule a task
echo "Timecop alert at $(date)" >> /tmp/timecop_alert.log | at now + 2 minutes
โ
Step 3: View and manage at
jobs
atq # View queue
atrm <job> # Remove job
โป๏ธ 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
#!/bin/bash
echo "Anomaly scan run at $(date)" >> /tmp/timecop_scan.log
โ Step 2: Make the script executable
chmod +x ~/timecop/scripts/scan.sh
โ Step 3: Schedule with crontab
crontab -e
Add this line:
* * * * * /home/yourusername/timecop/scripts/scan.sh
Feel free to set the schedule as you prefer. I set to run every minute in order to confirm the
cronjob
executes.If you find your
cronjob
is not running. Thecron
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
๐ Example:
15 14 1 * * /home/max/run_report.sh
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
#!/bin/bash
echo "System cleanup at $(date)" >> /var/log/timecop_cleanup.log
โ Step 2: Set executable permission
sudo chmod +x /etc/cron.weekly/timecop_cleanup.sh
๐ Scene 4: Investigating Scheduled Missions
โ View user's cron jobs
crontab -l
โ View system crontab
cat /etc/crontab
โ View scripts in system cron folders
ls /etc/cron.daily/
ls /etc/cron.weekly/
โ View logs (RHEL-specific)
sudo grep CRON /var/log/cron
โ
View at
jobs
atq
๐ค 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
๐ก 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
#!/bin/bash
echo "Time jump occurred at $(date)" >> /tmp/timecop_jump.log
โ Make it executable
chmod +x ~/timecop/scripts/test_jump.sh
โ
Run once with at
echo "/home/yourusername/timecop/scripts/test_jump.sh" | at now + 1 minute
โ
Schedule via crontab
crontab -e
# Add:
*/1 * * * * /home/yourusername/timecop/scripts/test_jump.sh
๐งพ 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.
Top comments (0)