Ravindra BagaleCourses & study guides

6. Linux Advanced Commands

6.9 Scheduling Tasks: cron

 ┌──────── 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

Cron job manually chalto pan cron madhun nahi – ha khup students cha problem. Karan cron la chhota PATH milto. Full paths vapra (/usr/bin/tar) aani output log la redirect kara: >> /home/ubuntu/backup.log 2>&1. Aadhi command haatane chalva, mag schedule kara.

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.