Linux menyediakan beberapa mekanisme untuk menjalankan tugas terjadwal (scheduled tasks).
Yang paling umum adalah cron (via crontab
).
crond
): service yang membaca konfigurasi dan mengeksekusi task. /var/log/syslog
(Debian/Ubuntu) atau /var/log/cron
(RHEL/CentOS). Format umum (5 field + perintah):
# ┌───────────── menit (0 - 59)
# │ ┌───────────── jam (0 - 23)
# │ │ ┌───────────── hari dalam bulan (1 - 31)
# │ │ │ ┌───────────── bulan (1 - 12)
# │ │ │ │ ┌───────────── hari dalam minggu (0 - 7) (Minggu=0/7)
# │ │ │ │ │
# * * * * * command yang akan dijalankan
Contoh:
30 2 * * * /usr/local/bin/backup.sh
👉 Jalankan backup.sh
setiap hari pukul 02:30.
crontab -e
crontab -l
crontab -r
sudo crontab -u www-data -e
/etc/crontab
→ crontab global (dengan field tambahan "user") /etc/cron.d/*
→ file tambahan untuk cron job spesifik /etc/cron.daily/
, /etc/cron.weekly/
, /etc/cron.hourly/
→ folder berisi script yang dijalankan sesuai periodenya*
→ semua nilai ,
→ daftar (contoh: 1,15,30
) -
→ rentang (contoh: 1-5
) /
→ step (contoh: */10
= setiap 10 menit) Shortcut:
@reboot
→ jalankan saat boot@hourly
→ setiap jam@daily
→ setiap hari (00:00)@weekly
→ setiap minggu (Minggu 00:00)@monthly
→ setiap bulan (tgl 1 jam 00:00)@yearly
/ @annually
→ setiap tahun (1 Jan 00:00)Contoh:
@reboot /usr/bin/python3 /opt/app/startup.py
0 1 * * * /usr/local/bin/backup_db.sh >> /var/log/backup.log 2>&1
*/5 * * * * /usr/local/bin/check_service.sh
0 8 * * 1 /usr/local/bin/healthcheck.sh | mail -s "Weekly Report" [email protected]
0 3 * * * find /tmp -type f -mtime +7 -exec rm -f {} \;
SHELL=/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
[email protected]
0 2 * * * /usr/local/bin/backup.sh
Cek log cron:
tail -f /var/log/syslog # Debian/Ubuntu
tail -f /var/log/cron # RHEL/CentOS
Pastikan script dapat dieksekusi manual.
Selalu gunakan path absolut untuk command dan file.
Tambahkan logging output (>> /var/log/script.log 2>&1
).
crontab -e
untuk user spesifik → hindari semua job ditaruh di root. PATH
, SHELL
) jelas di crontab. lockfile
atau tool seperti flock
untuk mencegah job overlap. crontab
mengatur jadwal tiap user, sedangkan /etc/crontab
dan /etc/cron.d
untuk sistem. * * * * *
, gunakan logging, dan ikuti best practices agar job terjadwal andalan dan aman di production.