ESC
Sintassi Cron

Field Positions

Field Allowed Values Special Chars Example Description
Minute 0 - 59 * , - / 30 Minute of the hour (0=on the hour, 30=half past)
Hour 0 - 23 * , - / 14 Hour of the day in 24-hour format (0=midnight, 14=2pm)
Day of Month 1 - 31 * , - / ? L W 15 Day of the month; not all months have 31 days
Month 1 - 12 or JAN-DEC * , - / 6 Month; names (JAN-DEC) are supported in most cron implementations
Day of Week 0 - 7 or SUN-SAT * , - / ? L # 1 0 and 7 both represent Sunday; 1=Monday ... 6=Saturday

Special Characters

Field Allowed Values Special Chars Example Description
* Any value * * * * * every Match any value in this field — wildcard for all values
, Value list 1,15 * * * * list Specify multiple values; runs at minute 1 AND minute 15
- Range 9-17 * * * * range Define range; 9-17 in hour field means every hour from 9am to 5pm
/ Step value */5 * * * * step Step through range; */5 in minute field means every 5 minutes
? No specific value 0 12 ? * MON ignore Used in day-of-month or day-of-week to say 'don't care' (Quartz/Spring)
L Last 0 0 L * * last Last day of month or last weekday; L in day-of-week = Saturday (Quartz)
W Nearest weekday 0 9 15W * * weekday Nearest weekday to given day of month (Quartz/Spring only)
# Nth weekday 0 10 * * 2#1 nth Nth occurrence of weekday in month; 2#1 = first Tuesday (Quartz)

Common Examples

Field Allowed Values Special Chars Example Description
* * * * * Every minute * * * * * /path/to/cmd 60x/hr Run command every minute — most frequent standard cron interval
0 * * * * Every hour 0 * * * * /path/cmd 24x/day Run at minute 0 of every hour (top of the hour)
0 0 * * * Daily at midnight 0 0 * * * /backup.sh 1x/day Run once per day at midnight (00:00)
0 9 * * 1-5 Weekdays 9am 0 9 * * 1-5 /cmd Mon-Fri Run at 9am Monday through Friday only
0 0 * * 0 Weekly Sunday 0 0 * * 0 /weekly.sh 1x/week Run at midnight every Sunday
0 0 1 * * Monthly 0 0 1 * * /monthly.sh 1st of month Run at midnight on the 1st day of each month
0 0 1 1 * Yearly 0 0 1 1 * /yearly.sh Jan 1st Run at midnight on January 1st each year
*/5 * * * * Every 5 minutes */5 * * * * /poll.sh 12x/hr Run every 5 minutes using step value
*/15 9-17 * * 1-5 Every 15 min business hrs */15 9-17 * * 1-5 /cmd business hrs Every 15 minutes between 9am-5pm, weekdays
0 2 * * * Daily at 2am 0 2 * * * /backup.sh 2:00 AM Common time for maintenance tasks — low traffic period
30 23 * * 5 Friday 11:30pm 30 23 * * 5 /cmd weekly Specific day and time each week
0 */6 * * * Every 6 hours 0 */6 * * * /sync.sh 4x/day Run at 00:00, 06:00, 12:00, and 18:00 each day

Predefined Shortcuts

Field Allowed Values Special Chars Example Description
@yearly 0 0 1 1 * @yearly /cmd Jan 1 00:00 Run once per year; same as @annually
@annually 0 0 1 1 * @annually /cmd Jan 1 00:00 Alias for @yearly
@monthly 0 0 1 * * @monthly /cmd 1st 00:00 Run once per month at midnight on first day
@weekly 0 0 * * 0 @weekly /cmd Sun 00:00 Run once per week at midnight on Sunday
@daily 0 0 * * * @daily /cmd 00:00 Run once per day at midnight; same as @midnight
@midnight 0 0 * * * @midnight /cmd 00:00 Alias for @daily
@hourly 0 * * * * @hourly /cmd top of hour Run once per hour at the beginning of each hour
@reboot (on startup) @reboot /start.sh on boot Run once at system startup; not supported in all cron implementations

Domande Frequenti

Un'espressione cron standard ha 5 campi separati da spazi: minuto (0-59), ora (0-23), giorno del mese (1-31), mese (1-12), giorno della settimana (0-7). Si legge da sinistra a destra: '30 14 * * 1' significa 'alle 14:30 in qualsiasi giorno di qualsiasi mese, ma solo il lunedì'. L'asterisco * significa 'qualsiasi valore', quindi '* * * * *' viene eseguito ogni minuto.

cron è il daemon (servizio in background) che legge ed esegue i lavori pianificati. crontab (tabella cron) è il file che memorizza il programma, e anche il comando per modificarlo. Usa crontab -e per modificare i tuoi cron job, crontab -l per elencarli, e crontab -r per rimuoverli tutti. Ogni utente ha il proprio file crontab.

Cause comuni: (1) Problema di PATH — cron viene eseguito con PATH minimo; usa percorsi completi come /usr/bin/php. (2) Permessi — lo script deve essere eseguibile (chmod +x). (3) Nessuna nuova riga alla fine del file crontab. (4) Output a /dev/null che nasconde gli errori — reindirizza temporaneamente stderr: * * * * * /cmd >> /tmp/cron.log 2>&1. (5) Variabili d'ambiente non impostate.