# 4.9 cron ने काम ठरलेल्या वेळी चालवा

Source: https://ravindrabagale.com/mr/aws/ch04-linux-advanced-commands/4-9-scheduling-tasks-cron.html
Language: mr (Marathi with English technical terms)

cron schedule नुसार command run करतो. crontab -e edit; crontab -l list.

 ┌──────── 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 | अर्थ

 | */5 * * * * | दर 5 minutes

 | 0 2 * * * | रोज 02:00

 | 30 9 * * 1-5 | सोमवार–शुक्रवार 09:30

 | 0 0 1 * * | महिन्याच्या पहिल्या दिवशी मध्यरात्री

 | @reboot | Boot वेळी एकदा

Mauli Dairy site चा रोज 01:30 backup: आधी mkdir -p ~/backups, मग crontab मध्ये:

30 1 * * * tar -czf $HOME/backups/site-$(date +\%F).tar.gz /var/www/html

crontab मध्ये % विशेष आहे म्हणून \% लिहितो. Production entry मध्ये tools/script चे absolute paths आणि output log वापरा.

AL2023 मध्ये cron

Default install नसेल तर sudo yum install -y cronie, sudo service crond start, sudo systemctl enable crond. systemd timers हा दुसरा पर्याय. Server timezone timedatectl ने तपासा; अनेक cloud images UTC वापरतात. संपूर्ण server IST करायचा ठरवला तर sudo timedatectl set-timezone Asia/Kolkata.

Lab

*/5 * * * * date >> $HOME/cron-test.log जोडा. साधारण 10 minutes नंतर log entries पाहा; schedule boundary नुसार संख्या बदलू शकते. काम झाल्यावर ती test line काढून crontab -l तपासा.

Minute, hour, day-of-month, month, weekday हा पाच fields चा क्रम लक्षात ठेवा.

रवींद्र बागले यांची tip

आधी command हाताने run करा. Cron ची PATH छोटी असते आणि shell interactive नसतो. /usr/bin/tar किंवा /home/ubuntu/backup.sh असे full paths आणि >> /home/ubuntu/backup.log 2>&1 वापरा.
