How to run cron jobs every 5, 10, or 30 seconds?

Better Stack Team
Updated on October 5, 2023

By default, cron checks crontabs for cronjobs every minute. If you want to run a job every n seconds you need to use a simple workaround.

🔭 Want to get alerted when your Cron doesn’t run correctly?

Go to Better Stack and start monitoring in 5 minutes.

Specify multiple jobs with offsets

The easiest way to run a job every n seconds is to run a job every minute and, and sleep in a loop in n second intervals.

Every 5 seconds

script.sh
i=0

while [ $i -lt 12 ]; do # 12 five-second intervals in 1 minute
  command/to/run & #run your command
  sleep 5
  i=$(( i + 1 ))
done
/etc/crontab
* * * * * script.sh

Every 10 seconds

script.sh
i=0

while [ $i -lt 6 ]; do # 6 ten-second intervals in 1 minute
  command/to/run & #run your command
  sleep 10
  i=$(( i + 1 ))
done
/etc/crontab
* * * * * script.sh

Every 15 seconds

script.sh
i=0

while [ $i -lt 4 ]; do # 4 ten-second intervals in 1 minute
  command/to/run & #run your command
  sleep 15
  i=$(( i + 1 ))
done
/etc/crontab
* * * * * script.sh

Every 30 seconds

script.sh
i=0

while [ $i -lt 2 ]; do # 2 ten-second intervals in 1 minute
  command/to/run & #run your command
  sleep 30
  i=$(( i + 1 ))
done
/etc/crontab
* * * * * script.sh

Every 30 seconds (different way)

/etc/crontab
* * * * * script.sh
* * * * * sleep 30 ; script.sh

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github