Vezi pagina în română

IT Glossary

Cron job (scheduled task): what it is and how to write one

A command the server starts on its own at fixed times — the nightly backup, the daily report, the monthly invoicing — with nobody pressing a button.

7
sections
7
questions answered

A cron job is a command a server runs on its own on a fixed schedule, written in five fields (minute, hour, day of month, month, day of week): 0 3 * * * means every day at 03:00. The name comes from the Greek chronos (time): the cron program on Unix and Linux checks its list of tasks, called the crontab, every minute, and the format is described in the POSIX standard (IEEE Std 1003.1-2024, the crontab utility).

Monthly invoicing, stock synchronisation, reminder emails, database housekeeping: behind them sit, as a rule, cron jobs. For your company they are the cheapest form of automation, since processes happen on time without depending on anyone's memory or availability. The question for your supplier: what happens when a job fails silently? With no alerting, you discover that the backup stopped running three months ago on the day you need it.

Why it matters for your business

Processes that never get forgotten

Reports, synchronisations and backups execute on schedule regardless of holidays, resignations or a chaotic week — reliability stops depending on one person's discipline.

Heavy work moved outside business hours

Large processing runs happen at night when servers are idle and customers are asleep, so during the day the application stays fast for real users.

Automation at almost zero cost

The mechanism comes built into any Linux server and hosting control panel, so automating a repetitive process costs only writing and testing the script.

Cron syntax: the five fields

Minute: 0–59. Hour: 0–23. Day of month: 1–31. Month: 1–12 (or jan–dec).

Day of week: 0–6, where 0 is Sunday (cronie also accepts 7 for Sunday, and mon–sun).

Operators: * means any value, a comma a list (1,15), a hyphen a range (1-5), a slash a step (*/15 is every 15 minutes). Note that */35 in the minute field runs at :00 and :35, not every 35 minutes, because the step applies within the hour.

Shortcuts: @hourly = 0 * * * *, @daily = 0 0 * * *, @weekly = 0 0 * * 0, @monthly = 0 0 1 * *, @yearly = 0 0 1 1 *, and @reboot runs once at server start. Steps and shortcuts are extensions from Vixie cron and cronie, not part of POSIX: they work on Linux servers, but check before relying on them elsewhere.

The day trap: if you set both the day of month and the day of week, the job runs when either one matches, not both. 30 4 1,15 * 5 means 04:30 on the 1st, on the 15th and every Friday.

Cron job examples used in companies

*/15 * * * * — every 15 minutes: stock synchronisation between the online shop and the inventory system.

0 3 * * * — daily at 03:00: database backup, while the server is idle.

0 8 * * 1-5 — Monday to Friday at 08:00: the sales report emailed before the working day starts.

0 0 1 * * (same as @monthly) — first day of the month: recurring invoices.

0 2 * * 0 — Sunday at 02:00: database cleanup and deletion of temporary files.

@reboot — once, after the server restarts: starting a service that does not start on its own.

Want an estimate for your project?

Message us on WhatsApp. We reply the same working day, and the first 30-minute call is free.

Book a free call

How to create a cron job

Careful: crontab -r deletes the entire list of tasks without asking. Save it first (crontab -l > crontab.bak).

Time zone: cron uses the server's time zone, and many servers run on UTC. For example, a job at 03:00 UTC runs at 05:00 in winter and 06:00 in summer in Romania. Cronie accepts the CRON_TZ variable (for instance CRON_TZ=Europe/Bucharest) for a specific zone.

WordPress has its own mechanism, WP-Cron, which only starts when someone loads a page: a task scheduled for 14:00 may run at 17:00 on a low-traffic site. The fix in the WordPress documentation: put define( 'DISABLE_WP_CRON', true ); in wp-config.php and create a real cron job that calls wp-cron.php, for example every 15 minutes.

How to check that a cron job ran

The log: on Debian and Ubuntu usually grep CRON /var/log/syslog or journalctl -u cron; on RHEL and Fedora (cronie) usually /var/log/cron. The log shows that the job started, not that it succeeded.

The output: if you do not redirect it, POSIX says the output is mailed to the owner of the crontab; the MAILTO variable sets the address, and MAILTO="" turns the mail off. Many servers have no mail configured, so redirect the output to a file: >> /var/log/backup.log 2>&1.

Heartbeat: at the end, the job calls a URL at a service such as Healthchecks.io (hc-ping.com/<uuid>, with /start and /fail variants), and the service alerts when the signal is later than the grace time. Cronitor works the same way.

Overlap: a job that takes longer than its interval starts a second time before the first run ends. Use a lock (flock) or, in Kubernetes, concurrencyPolicy: Forbid. Failure and lateness alerts belong in your server monitoring.

Cron in the cloud, Kubernetes and Windows

AWS EventBridge Scheduler: cron(minutes hours day-of-month month day-of-week year), so six fields, with day of week from 1 to 7 (SUN = 1) and a ? required in one of the two day fields. It accepts any IANA time zone and has 60-second precision; besides cron() there is rate() for fixed intervals (e.g. rate(15 minutes)) and at() for a single run.

Google Cloud Scheduler uses unix-cron with five fields and UTC by default; its documentation recommends UTC because of daylight saving. Azure Logic Apps uses no cron text: the Recurrence trigger takes a frequency (second, minute, hour, day, week, month) and an interval.

Kubernetes has the CronJob object, with the same five fields and shortcuts such as @daily; concurrencyPolicy (Allow, Forbid or Replace) decides what happens when one run overlaps the next.

Windows does not use crontab; it has Task Scheduler.

Frequently asked questions

What does that cryptic notation like 0 3 * * * actually mean?

Five fields that fix the schedule: minute, hour, day of month, month, day of week. So 0 3 * * * means daily at three in the morning, and */15 * * * * means every fifteen minutes. Cryptic at first sight, but it is the format of the POSIX standard on Unix and Linux servers.

How do I find out whether a cron job has failed?

By default you often do not — the job dies quietly. Check the log (usually grep CRON /var/log/syslog on Debian and Ubuntu), and for a lasting fix have every job check in with a heartbeat service such as Healthchecks.io or Cronitor, which alerts when the check-in stops arriving.

Are there cron jobs in the cloud, without our own server?

Yes: AWS EventBridge Scheduler, Google Cloud Scheduler and Azure Logic Apps start a function, a message queue or an HTTP address on a schedule, with no server to maintain. The syntax differs from crontab: AWS uses six fields, and Azure Logic Apps uses no cron text, just a frequency and an interval.

How do you create a cron job?

On Linux, run crontab -e, write a line with the five schedule fields followed by the command, and save; crontab -l lists the result so you can check it. On shared hosting, use the Cron Jobs section of cPanel or DirectAdmin and fill in the schedule and command in the form.

Can a cron job run every 30 seconds?

Not directly, because the minute is the smallest unit. The workaround is two lines, * * * * * command and * * * * * sleep 30; command. For work that must happen more often than once a minute, the right tool is a permanent worker process or a message queue.

What is the Windows equivalent of cron?

Task Scheduler. From the command line you create a task with schtasks /create, choosing the frequency with /sc (minute, hourly, daily, weekly, monthly) and the time with /st, for example /sc daily /st 03:00. Crontab syntax does not work there.

What is the difference between cron, crontab and a cron job?

Cron is the program (daemon) that runs in the background and checks the schedule every minute. The crontab is the table of tasks, plus the command that edits it. A cron job is a single line in that table: a schedule and a command to run.