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

  • 44

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.

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

Was this answer helpful?

« Back