Operations 3 min read

Run Linux Cron Jobs Every Second Using a Bash Loop and Sleep

This guide explains how to achieve second‑level scheduling on Linux by combining a minute‑based crontab entry with a custom Bash script that loops and uses sleep, including a PHP test task example and step‑by‑step setup instructions.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
Run Linux Cron Jobs Every Second Using a Bash Loop and Sleep

Linux crontab's smallest execution unit is one minute. To run a task every second, combine a minute‑based crontab entry with a custom script that loops and sleeps.

Idea

Define the actual task separately, then create a script that repeatedly executes the task with a sleep interval. Schedule the script in crontab to run each minute.

Example

1. Define the target task

Here a simple PHP script writes the current timestamp to a log file.

<?php
file_put_contents('/root/run.log', date('Y-m-d H:i:s')."
", FILE_APPEND);
?>

2. Create the execution script

Save the following Bash script as /root/test.sh. Set the desired interval (in seconds) with step, which must not exceed 60.

#!/bin/bash
# interval in seconds (cannot be greater than 60)
step=2

for (( i=0; i<60; i+=step )); do
    php /root/test.php
    sleep $step
done
exit 0

3. Add the crontab entry

Edit the crontab with crontab -e and add:

* * * * * /root/test.sh

Restart the cron service:

service crond restart

4. Verify the result

Monitor the log file:

tail -f /root/run.log

You will see timestamps printed every step seconds.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxSchedulingPHPcronBashscript
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.