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.
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 03. Add the crontab entry
Edit the crontab with crontab -e and add:
* * * * * /root/test.shRestart the cron service:
service crond restart4. Verify the result
Monitor the log file:
tail -f /root/run.logYou will see timestamps printed every step seconds.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
