How to run scripts in Linux faster than Cron does

less than 1 minute read

Cron is an amazing┬ájob scheduler. But, when it comes to running your scripts in less than every minute, it can’t help.

With Cron, you can run your scripts every minute, but not in shorter period of time.

This problem can be solved with simple bash script. For example, if you need to run a PHP script on every 20 seconds, you can create a bash script like this:

#!/bin/bash
#Name:myscript.sh
#Desc:Run script in every 20 seconds
while (sleep 20 && php /path_to_your_script/your_script_name.php) &
do
 wait $!
done

Then, make script executable, and add it to the system startup. That’s all.

 

 


If you liked the post, we should get connected - follow me on Twitter

4 Comments How to run scripts in Linux faster than Cron does

  1. linux friend

    Nice, but why so complicated?
    while sleep 20; do php /script; done
    is enough. No need for backgrounding and wait $!

    Reply
  2. Tom V

    Cron can cetianly do this, you jsut put in sleeps on the cron entries into a cron.d (so you can keep yourself sane).

    So for an every 20 second cron:

    * * * * * script
    * * * * * sleep 20; script
    * * * * * sleep 40; script

    It also best to do this in something like puppet or chef so you can monitor and modify them easily. We created a custom type which wraps cron in puppet for this, so you can have subinterval for minutes.

    Reply
  3. Simone Freddio

    I wrote a little tool that can schedule jobs at milliseconds interval. It is at a good level of reliability, i use it in production enviroment. I have also implemented an overlapping guard, if previous jobs is still running, you can configure the scheduler to not start a new process until the previous one is running. You can take a look at https://github.com/sioux1977/scheduler/wiki. In every case… a great post.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *