How To Run A Unix Script Every 15 Seconds?

Published August 20, 2024

Problem: Running Unix Scripts Frequently

Running a Unix script at short, regular intervals can be difficult. Executing a script every 15 seconds needs a method beyond standard scheduling tools like cron, which usually works on a per-minute basis.

Solution: Using Cron with Sleep Commands

Setting up the Cron Job

To run a script every 15 seconds using cron, you can create a cron entry that runs every minute and use multiple sleep commands. This method lets you run the script four times within each minute, achieving the 15-second interval.

To set up the cron job:

  1. Open your crontab file using the command crontab -e.
  2. Add these lines to the file:
* * * * * /path/to/your/script
* * * * * sleep 15; /path/to/your/script
* * * * * sleep 30; /path/to/your/script
* * * * * sleep 45; /path/to/your/script
  1. Save and close the file.

Example Cron Configuration

Here's how the cron entry works:

  1. * * * * * /path/to/your/script: This line runs the script at the start of every minute.
  2. * * * * * sleep 15; /path/to/your/script: This line waits for 15 seconds, then runs the script.
  3. * * * * * sleep 30; /path/to/your/script: This line waits for 30 seconds, then runs the script.
  4. * * * * * sleep 45; /path/to/your/script: This line waits for 45 seconds, then runs the script.

The * * * * * at the start of each line tells cron to run the command every minute. The sleep commands add delays, allowing the script to run at 0, 15, 30, and 45 seconds past each minute.

Replace /path/to/your/script with the actual path to your script file. Make sure your script is executable by running chmod +x /path/to/your/script.

This method offers a simple way to run your script every 15 seconds using cron, and it will continue to run after system reboots as long as the cron daemon is active.

Tip: Logging Cron Job Execution

To monitor the execution of your cron job, you can add logging to your script. Here's a simple way to do it:

* * * * * /path/to/your/script >> /path/to/logfile.log 2>&1
* * * * * sleep 15; /path/to/your/script >> /path/to/logfile.log 2>&1
* * * * * sleep 30; /path/to/your/script >> /path/to/logfile.log 2>&1
* * * * * sleep 45; /path/to/your/script >> /path/to/logfile.log 2>&1

This will append the output of each execution to the specified log file, helping you track when the script runs and catch any errors.

Alternative Solution: Using a Loop Script

Creating a Continuous Loop

Another way to run a script every 15 seconds is to create a shell script with an infinite loop. This method gives you more control over the execution timing and can be more precise than the cron-based solution.

Here's a basic shell script that creates a continuous loop:

#!/bin/bash

while true
do
    # Your script or command here
    /path/to/your/script

    # Sleep for 15 seconds
    sleep 15
done

This script:

  • Uses a while true loop to run indefinitely
  • Executes your script or command
  • Waits for 15 seconds before the next iteration

Save this script with a .sh extension (e.g., loop_script.sh) and make it executable with chmod +x loop_script.sh.

Tip: Logging Loop Execution

Add timestamp logging to your loop script to track execution times:

#!/bin/bash

while true
do
    echo "$(date): Executing script"
    /path/to/your/script
    echo "$(date): Execution complete"
    sleep 15
done

This helps you monitor the script's activity and timing.

Running the Loop Script as a Background Process

To keep the loop script running continuously, even after you log out or reboot the system, you can run it as a background process.

To start the script at boot time:

  1. Edit the /etc/rc.local file (create it if it doesn't exist):
sudo nano /etc/rc.local
  1. Add the following line before the exit 0 statement:
/path/to/loop_script.sh &

The & at the end runs the script in the background.

You can also use nohup or screen to keep the script running:

  • Using nohup:

    nohup /path/to/loop_script.sh &
  • Using screen:

    screen -dmS script_session /path/to/loop_script.sh

Both nohup and screen allow the script to continue running even if you close the terminal session.

This loop script method provides a direct way to run your script every 15 seconds and maintains execution across system reboots.