Chapter 4: Linux Advanced Commands
4.9 Scheduling tasks: cron
In short: cron runs commands automatically on a schedule.
cron runs commands automatically on a schedule. Edit your table with crontab -e, list with crontab -l.
┌──────── 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 do this with me. Example – the Mauli Dairy website should be backed up every night at 1:30 AM. Run crontab -e and add this line:
आता टर्मिनल उघडा आणि माझ्यासोबत करा. उदाहरण – Mauli Dairy ची वेबसाइट रोज रात्री 1:30 AM ला बॅकअप करायची. crontab -e चालवा आणि ही लाइन ॲड करा:
अब टर्मिनल खोलो और मेरे साथ करो. उदाहरण – Mauli Dairy की वेबसाइट का रोज़ रात 1:30 AM पर बैकअप लेना है. crontab -e चलाओ और ये लाइन ऐड करो:
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.
Ravindra Bagale's Tip
Before scheduling any cron job, run the same command by hand first and check it. Cron runs with a very small PATH and without an interactive shell, so always use full paths (/usr/bin/tar, /home/ubuntu/backup.sh) and send the output to a log file: >> /home/ubuntu/backup.log 2>&1. Remember this!
Ravindra Bagale's Tip – मराठी
कोणताही cron job schedule करण्याआधी तीच command आधी हाताने चालवून check करा. Cron खूप छोट्या PATH सोबत आणि interactive shell शिवाय चालतो, म्हणून नेहमी full paths वापरा (/usr/bin/tar, /home/ubuntu/backup.sh) आणि output log file मध्ये पाठवा: >> /home/ubuntu/backup.log 2>&1. लक्षात ठेवा!
Ravindra Bagale's Tip – हिंदी
कोई भी cron job schedule करने से पहले वही command पहले हाथ से चलाकर check करो. Cron बहुत छोटे PATH के साथ और interactive shell के बिना चलता है, इसलिए हमेशा full paths इस्तेमाल करो (/usr/bin/tar, /home/ubuntu/backup.sh) और output log file में भेजो: >> /home/ubuntu/backup.log 2>&1. याद रखो!
Ravindra Bagale's Tip
Many students type crontab -e on AL2023 and get command not found – because cronie is not even installed. First run sudo yum install -y cronie, sudo service crond start and sudo systemctl enable crond. And the server runs on UTC – if you want 1:30 AM IST, set the timezone to Asia/Kolkata.
Ravindra Bagale's Tip – मराठी
बरेच students AL2023 वर crontab -e type करतात आणि command not found येतं – कारण cronie install च नाही. आधी sudo yum install -y cronie, sudo service crond start आणि sudo systemctl enable crond. आणि server UTC वर असतो – 1:30 AM IST पाहिजे असेल तर timezone Asia/Kolkata set करा.
Ravindra Bagale's Tip – हिंदी
बहुत से students AL2023 पर crontab -e type करते हैं और command not found आता है – क्योंकि cronie install ही नहीं है. पहले sudo yum install -y cronie, sudo service crond start और sudo systemctl enable crond. और server UTC पर होता है – 1:30 AM IST चाहिए तो timezone Asia/Kolkata set करो.
Lab
Chala, crontab -e ne */5 * * * * date >> $HOME/cron-test.log hi line add kara, aani 10 minitani cat ~/cron-test.log madhe don lines aalya ka te bagha. Kaam zalyavar crontab -e madhun ti line kadhun taka aani crontab -l ne check kara.
Got it? Five fields – minute, hour, day, month, weekday – and full paths. Now let's move on to environment variables.
समजलं का? पाच फील्ड्स – minute, hour, day, month, weekday – आणि फुल पाथ्स. आता पुढे जाऊया environment variables कडे.
समझ आया? पाँच फ़ील्ड्स – minute, hour, day, month, weekday – और फ़ुल पाथ्स. अब आगे चलते हैं environment variables की तरफ़.