How do I run a cron job every 30 seconds?

On this page

The smallest unit in a Unix cron expression is a minute, so a single line cannot run every 30 seconds. Two lines can: one runs the job at the start of the minute, the other waits 30 seconds first. Better still, use a scheduler that supports seconds.

What are the options?

  • Two crontab lines, the second one starting with sleep 30.
  • A systemd timer with OnCalendar=*:*:0/30, which understands seconds.
  • An in-process scheduler with second precision, such as node-cron, sidekiq-cron or Laravel's everySecond tasks.
  • pg_cron, which accepts an interval of 1 to 59 seconds on its own.
  • A long-running worker with its own loop, which is often the right design for work that never really stops.
crontab
* * * * * /usr/local/bin/poll.sh
* * * * * sleep 30; /usr/local/bin/poll.sh

The every 30 seconds page has the same workaround with its daylight saving behavior.

How should I monitor a job this frequent?

A heartbeat check watches a schedule; it is not built to receive a ping every few seconds. CronMissed accepts up to five recorded pings a minute per check and has a minimum period of 60 seconds, so for a 30 second job ping once a minute, from one of the two lines, or after every Nth pass of a loop.

crontab
* * * * * /usr/local/bin/poll.sh && curl -fsS --retry 3 https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10
* * * * * sleep 30; /usr/local/bin/poll.sh

Set a simple check with a period of 60 seconds and a grace of two or three minutes. That alerts within minutes when polling stops, which is the right scale for a job that runs 2,880 times a day.

What goes wrong at this frequency?

  • Overlaps: a run that takes longer than 30 seconds meets the next one. Use a lock.
  • Process churn: starting a new interpreter twice a minute adds up. A resident worker avoids it.
  • Log growth: two runs a minute fill a log quickly. Log only what you will read.

Sources

Questions and answers

Is a one-second schedule possible?
With a scheduler that supports it, yes. For monitoring, a heartbeat once a minute is almost always enough; per-second health belongs in metrics.
Can CronMissed check every 30 seconds?
The shortest period of a check is 60 seconds, and a check records up to five pings a minute. Ping once a minute and let the job do the work in between.
Know the moment a job goes quiet

Create a check, add one curl to the job, and CronMissed alerts you when a ping is late or reports a failure.

Start monitoring