6.9 Scheduling Tasks: cron
Also in the AWS course: this lesson is the same as 4.9 Scheduling tasks: cron in the AWS course, which is the main version of this page.
┌──────── minute (0-59)
│ ┌────── hour (0-23)
│ │ ┌──── day of month (1-31)
│ │ │ ┌── month (1-12)
│ │ │ │ ┌ day of week (0-7, 0 and 7 = Sunday)
│ │ │ │ │
* * * * * command
| Schedule | Meaning |
|---|---|
*/5 * * * * |
Every 5 minutes |
0 2 * * * |
Every day at 02:00 |
30 9 * * 1-5 |
09:30 Monday to Friday |
0 0 1 * * |
Midnight on the 1st of every month |
@reboot |
Once at every boot |
Now open your terminal and try this with me. Example — back up the website every night at 1:30 AM. Run crontab -e and add this line:
30 1 * * * tar -czf $HOME/backups/site-$(date +\%F).tar.gz /var/www/html
(% has a special meaning in crontab, so it is written as \%. Create the folder first with mkdir -p ~/backups.)
cron on Amazon Linux 2023
AL2023 does not ship cron by default. Install it: sudo yum install -y cronie, then sudo service crond start and sudo systemctl enable crond. (systemd timers are the modern alternative.) The server clock is UTC by default — set India time with sudo timedatectl set-timezone Asia/Kolkata.
Why this matters for security
Cron is a favourite persistence (टिकून राहण्याची पद्धत) mechanism: an attacker adds a job that re-opens a reverse shell every minute. Check crontab -l for every user, /etc/crontab and /etc/cron.d/ during investigations. A root cron job that runs a world-writable script is also a privesc path.
Ravindra Bagale's Tip
A cron job runs manually but not from cron – that's a problem many students face. The reason is that cron gets a short PATH. Use full paths (/usr/bin/tar) and redirect the output to a log: >> /home/ubuntu/backup.log 2>&1. Run the command by hand first, then schedule it.
Ravindra Bagale's Tip – मराठी
Cron job हाताने चालतो पण cron मधून नाही – हा बऱ्याच students चा problem. कारण cron ला छोटा PATH मिळतो. Full paths वापरा (/usr/bin/tar) आणि output log ला redirect करा: >> /home/ubuntu/backup.log 2>&1. आधी command हाताने चालवा, मग schedule करा.
Ravindra Bagale's Tip – हिंदी
Cron job हाथ से चलता है पर cron से नहीं – यह बहुत से students की problem है. वजह यह है कि cron को छोटा PATH मिलता है. Full paths इस्तेमाल करो (/usr/bin/tar) और output को log में redirect करो: >> /home/ubuntu/backup.log 2>&1. पहले command हाथ से चलाओ, फिर schedule करो.
Practice task
Schedule a job that writes the date and uptime to ~/uptime.log every 5 minutes. After 15 minutes check the file, then remove the job.
Extended teaching notes
Why this lesson matters
Use this lesson to strengthen systems you own: understand the risk, practise safely in a lab, then write and apply the fix.
Apply the fix across stacks
Same class of bug, same idea in every language.
Java / Spring Boot
Parameterise queries, encode output, CSRF tokens, limited Actuator, dependency scanning.
ASP.NET Core
EF Core / parameterised SQL, Razor encoding, HTTPS, Data Protection.
Node.js / Express
helmet, parameterised SQL, npm audit, avoid eval and shell concat.
Python / Django / Flask / FastAPI
ORM, autoescape, pip-audit, Bandit; never shell=True with user input.
PHP / Laravel
PDO/Eloquent, Blade escaping, composer audit.
Go / Ruby
database/sql placeholders / ActiveRecord; html/template / ERB escaping; govulncheck / Brakeman.
nginx / Apache / Tomcat
Hide versions, TLS, security headers, least-open ports.
MySQL / PostgreSQL / MongoDB / Redis
Private networks, auth on, least-privilege users, no 0.0.0.0/0.
AWS / Azure / GCP
Roles not long-lived keys, private data stores, cloud audit logs on.
Mini case study
Suppose we are on a purple-team exercise for an Indian mid-size company (sample data). Red notes a technique from this lesson against a lab clone. Blue must show the control that stops it, the log that would have fired, and the ticket that hardens production. That is the standard this course trains you for.
Defender's worksheet (do this today)
- Write the lesson title, date and the exact lab target you are allowed to use.
- List three owned systems (VMs, apps, or cloud accounts) where this lesson applies.
- Run one safe check from the lesson on a lab system only. Paste the command and output.
- Write a two-sentence fix you would give a teammate.
- Name one related public advisory (CERT-In, CISA, NVD, or a vendor bulletin) with its date.
Indian workplace (sample data)
Suppose we are a junior analyst at Infosys, TCS, Wipro or a fintech SOC in Bengaluru/Pune. Turn this lesson into one checklist item with an owner, a due date and a place to store evidence (ticket + screenshot).
Global practice
Compare your notes with OWASP, CIS Benchmarks, NIST CSF/IR guidance or a cloud Well-Architected security question. Write five lines on what overlaps.
Everyday analogy
Explain the risk and the fix to a family member without jargon. If they can repeat the warning, you understand it well enough to teach juniors.
Key terms
- Control: a safeguard (patch, config, process, detection).
- Finding: a weakness backed by evidence.
- Remediation: the fix plus how you verified it.
- Scope: systems you have written permission to touch.
- Least privilege: only the access the task needs.
- Defence in depth: several layers so one failure does not end the story.
Common mistakes
- Skipping written permission "just for a quick scan".
- Practising on hostel, office or public targets without authorisation.
- Collecting tool output without writing the fix.
- Copying attack steps onto production to "verify". Defenders verify with versions, configs and patches.
- Publishing screenshots that contain secrets, cookies or internal hostnames.
Recap
- Evidence, fix, owner, date.
- Own lab only.
- Fix > spectacle.