How to Execute Less Than 1 Minute Interval Jobs Using Cron Time-Based Scheduler

When working with the Cron time-based scheduler in Linux, you might find yourself needing to execute tasks at intervals of less than one minute. However, the native capabilities of Cron do not allow for such granular scheduling. By default, Cron’s smallest interval is one minute. This can be limiting for certain applications where tasks need to be run every few seconds or at a more precise time interval. In this article, we will explore multiple methods to overcome this limitation and successfully schedule tasks at intervals of less than one minute.

In this tutorial you will learn:

  • How to execute tasks at intervals shorter than one minute using various methods
  • Step-by-step instructions for each method with example code
How to Execute Less Than 1 Minute Interval Jobs Using Cron Time-Based Scheduler
How to Execute Less Than 1 Minute Interval Jobs Using Cron Time-Based Scheduler
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Linux-based system
Software Cron, Bash, systemd
Other Basic knowledge of Linux command line
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Methods to Execute Less Than 1 Minute Interval Jobs

There are several methods to achieve job execution at intervals of less than one minute. Each method has its own advantages and is suitable for different use cases. Below are three methods that you can use.

  1. Using a While Loop in a Bash Script: One of the simplest ways to execute a task at intervals shorter than one minute is by using a while loop in a Bash script. This method involves writing a script that runs continuously, executing a command and then sleeping for a specified number of seconds.
    #!/bin/bash
    while true; do
      # Your command here
      sleep 10
    done
    

    In this method, the while true loop ensures that the script runs indefinitely. The sleep 10 command pauses the script for 10 seconds between each iteration, allowing the task to execute every 10 seconds. This method is simple but consumes more CPU resources as it runs continuously.

  2. Using systemd Timers:

    SYSTEMD TIMER PRECISION
    Systemd timers are convenient for scheduling tasks at sub-minute intervals, but they may not offer precise timing. Factors like system load, execution time, and timer granularity can introduce slight delays, making the interval slightly longer than specified. For critical tasks requiring exact timing, consider alternative solutions or adjust the interval accordingly.

    Below are the detailed steps to create a systemd service and a corresponding timer to execute your task at a precise interval.1. Create the Service File:
    – First, create a service file at /etc/systemd/system/mytask.service. This service file defines the actual task or script you want to run. Replace /path/to/your/script.sh with the actual path to your script.
    – In this example, the service is named mytask.service. The Description field provides a brief explanation of what the service does.
    – The ExecStart field specifies the command or script that will be executed by the service.

    [Unit]
    Description=Run My Task
    
    [Service]
    ExecStart=/path/to/your/script.sh
    

    2. Create the Timer File:
    – Next, create a corresponding timer file at /etc/systemd/system/mytask.timer. This timer file controls how often the task is run.
    – The OnBootSec=10 directive ensures that the timer starts 10 seconds after the system has booted up.
    – The OnUnitActiveSec=10 directive schedules the task to run every 10 seconds after the last execution of the service. Adjust this value to set your desired interval.
    – The WantedBy=timers.target field ensures that the timer is started automatically when the system boots.

    [Unit]
    Description=Run My Task Every 10 Seconds
    
    [Timer]
    OnBootSec=10
    OnUnitActiveSec=10
    
    [Install]
    WantedBy=timers.target
    

    3. Reload systemd and Enable the Timer:
    – Once both files are created, reload the systemd manager configuration to recognize the new service and timer by running:

    # systemctl daemon-reload

    – Then, enable the timer to start it automatically at boot:

    # systemctl enable mytask.timer

    – Finally, start the timer manually for the first time (optional):

    # systemctl start mytask.timer

    4. Check the Timer Status:
    – To verify that your timer is running correctly, you can check its status with:

    # systemctl status mytask.timer

    – This command will show you whether the timer is active and when it is scheduled to run next.



    5. Review Timer Logs:
    – You can also review the logs for both the service and the timer using the journalctl command:

    # journalctl -u mytask.service

    – This will provide details about the execution of the task and any potential issues.

    In this setup, the service unit defines the task to be executed, while the timer unit controls the frequency of execution. The OnUnitActiveSec=10 directive in the timer unit ensures that the task runs every 10 seconds. This method is more resource-efficient than using a while loop because it leverages systemd’s built-in capabilities for precise scheduling.

    Example of using systemd Timers to execute task with a frequency less than 1 minute
    Example of using systemd Timers to execute task with a frequency less than 1 minute
  3. Using a Crontab Hack with Sleep: Another method to execute tasks at intervals shorter than one minute is to utilize a Crontab entry in combination with the sleep command. This method allows you to stagger the execution of the task within the same minute.
    * * * * * /bin/bash -c 'command1; sleep 15; command2; sleep 15; command3'
    

    In this example, the Cron job runs every minute, but the sleep commands are used to delay the execution of subsequent commands. This effectively breaks the task into smaller intervals within the same minute. Although this method doesn’t provide true sub-minute scheduling, it can be useful for certain scenarios where exact timing is not critical.

Conclusion

While Cron’s default capabilities limit task scheduling to one-minute intervals, there are several methods to achieve more frequent execution. Whether you choose to use a while loop, systemd timers, or a clever combination of Cron and sleep, each approach offers a way to overcome this limitation. Depending on your specific requirements, you can select the method that best fits your needs and system resources.



Comments and Discussions
Linux Forum