How to Schedule a Cron Job to Run Every 10 Seconds in Linux: A Technical Guide


2 views

Cron is designed for minute-level scheduling, with the smallest time unit being 1 minute. This limitation makes it impossible to directly specify a 10-second interval in the standard cron syntax. However, there are several workarounds to achieve this frequency.

Create a script that runs your command in a loop with 10-second intervals:


#!/bin/bash
while true
do
    /path/to/your/command
    sleep 10
done

Then schedule this script to run at reboot in crontab:


@reboot /path/to/your/script.sh

Create six separate cron jobs that run at different second offsets within the minute:


* * * * * /path/to/your/command
* * * * * sleep 10; /path/to/your/command
* * * * * sleep 20; /path/to/your/command
* * * * * sleep 30; /path/to/your/command
* * * * * sleep 40; /path/to/your/command
* * * * * sleep 50; /path/to/your/command

For modern Linux systems with systemd, create a service and timer:


# /etc/systemd/system/10sec-job.service
[Unit]
Description=10 Second Job

[Service]
ExecStart=/path/to/your/command

# /etc/systemd/system/10sec-job.timer
[Unit]
Description=10 Second Timer

[Timer]
OnBootSec=10
OnUnitActiveSec=10
AccuracySec=1ms

[Install]
WantedBy=timers.target

Then enable and start the timer:


systemctl enable 10sec-job.timer
systemctl start 10sec-job.timer

When running jobs this frequently:

  • Monitor system resources and job execution time
  • Implement proper logging and error handling
  • Consider using flock to prevent overlapping executions
  • For critical tasks, implement a watchdog mechanism

For more advanced scheduling needs, consider:

  • inotifywait for filesystem event-based triggers
  • Python's schedule library
  • Node.js with node-cron
  • Java's ScheduledExecutorService

Traditional cron jobs in Linux have a minimum interval of 1 minute, defined by the * * * * * syntax. When you need finer-grained scheduling like every 10 seconds, we need to implement workarounds.

Here's the most common approach using sleep commands within a single cron job:

* * * * * /path/to/your/script.sh
* * * * * sleep 10; /path/to/your/script.sh
* * * * * sleep 20; /path/to/your/script.sh
* * * * * sleep 30; /path/to/your/script.sh
* * * * * sleep 40; /path/to/your/script.sh
* * * * * sleep 50; /path/to/your/script.sh

Create a shell script that handles the 10-second interval internally:

#!/bin/bash
while true
do
  /path/to/your/actual/command
  sleep 10
done

Then schedule this wrapper to run at boot or via cron:

@reboot /path/to/wrapper_script.sh

For systems using systemd, create two files:

1. Service file (/etc/systemd/system/10sec-job.service):

[Unit]
Description=10 Second Job Service

[Service]
ExecStart=/path/to/your/script.sh

2. Timer file (/etc/systemd/system/10sec-job.timer):

[Unit]
Description=Run script every 10 seconds

[Timer]
OnBootSec=10s
OnUnitActiveSec=10s

[Install]
WantedBy=timers.target
  • Ensure your script can handle potential overlaps if execution takes longer than 10 seconds
  • Monitor resource usage for very frequent jobs
  • Consider adding proper logging to track execution
  • For critical systems, implement proper error handling and notifications

While cron wasn't designed for sub-minute intervals, these methods provide reliable workarounds. Choose the approach that best fits your system architecture and requirements.