How to run a Linux script every few seconds under cron
From Computer Tyme Support Wiki
(→Even more precise with microsecond resolution) |
(→Overview) |
||
Line 1: | Line 1: | ||
= Overview = | = Overview = | ||
- | Did you ever want to run a program every few seconds under a linux, unix, bsd or osx cron script? Here's an elegant script that does just that | + | Did you ever want to run a program every few seconds, or even fractions of a second, under a linux, unix, bsd or osx cron script? Here's an elegant script that does just that. |
Features: | Features: | ||
* Run once per minute under cron | * Run once per minute under cron | ||
* Launches multiple programs in parallel | * Launches multiple programs in parallel | ||
- | * Multiple time periods supported simultaneously | + | * Multiple time periods supported simultaneously just by the directory name. |
- | + | ||
* It just works | * It just works | ||
* Simple, elegant, free | * Simple, elegant, free | ||
- | |||
- | |||
Even though this software is free if you find it really useful and you want to reward/encourage me you can email me an Amazon Gift Certificate to marc@perkel.com. | Even though this software is free if you find it really useful and you want to reward/encourage me you can email me an Amazon Gift Certificate to marc@perkel.com. | ||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
- | |||
== Even more precise with microsecond resolution == | == Even more precise with microsecond resolution == | ||
- | + | Tthe existence of the directory creates the schedule. This version supports microsecond resolution assuming your computer can handle it. It is the most accurate on start time and actually us a more simple implementation. Also the number of executions per minute does not have to to evenly divisible by 60. If you want to run something 17 times a minute - no problem. You can also run faster than once a second. | |
- | + | ||
Even though this has microsecond resolution probably 2400 per minute is as fast as you can practically go. | Even though this has microsecond resolution probably 2400 per minute is as fast as you can practically go. |
Revision as of 02:32, 17 February 2014
Overview
Did you ever want to run a program every few seconds, or even fractions of a second, under a linux, unix, bsd or osx cron script? Here's an elegant script that does just that.
Features:
- Run once per minute under cron
- Launches multiple programs in parallel
- Multiple time periods supported simultaneously just by the directory name.
- It just works
- Simple, elegant, free
Even though this software is free if you find it really useful and you want to reward/encourage me you can email me an Amazon Gift Certificate to marc@perkel.com.
Even more precise with microsecond resolution
Tthe existence of the directory creates the schedule. This version supports microsecond resolution assuming your computer can handle it. It is the most accurate on start time and actually us a more simple implementation. Also the number of executions per minute does not have to to evenly divisible by 60. If you want to run something 17 times a minute - no problem. You can also run faster than once a second.
Even though this has microsecond resolution probably 2400 per minute is as fast as you can practically go.
Keep in mind that this program takes up to 2 minutes to start.
#! /bin/sh # Microsecond Cron # Usage: cron-ms start # Copyright 2014 by Marc Perkel # docs at http://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_every_few_seconds_under_cron" # Free to use with attribution basedir=/etc/cron-ms if [ $# -eq 0 ] then echo echo "cron-ms by Marc Perkel" echo echo "This program is used to run all programs in a directory in parallel every X times per minute." echo "Think of this program as cron with microseconds resolution." echo echo "Usage: cron-ms start" echo echo "The scheduling is done by creating directories with the number of" echo "executions per minute as part of the directory name." echo echo "Examples:" echo " /etc/cron-ms/7 # Executes everything in that directory 7 times a minute" echo " /etc/cron-ms/30 # Executes everything in that directory 30 times a minute" echo " /etc/cron-ms/600 # Executes everything in that directory 10 times a second" echo " /etc/cron-ms/2400 # Executes everything in that directory 40 times a second" echo exit fi # If "start" is passed as a parameter then run all the loops in parallel # The number of the directory is the number of executions per minute # Since cron isn't accurate we need to start at top of next minute if [ $1 = start ] then for dir in $basedir/* ; do $0 ${dir##*/} 60000000 & done exit fi # Loops per minute and the next interval are passed on the command line with each loop loops=$1 next_interval=$2 # Sleeps until a specific part of a minute with microsecond resolution. 60000000 is full minute usleep $(( $next_interval - 10#$(date +%S%N) / 1000 )) # Run all the programs in the directory in parallel for program in $basedir/$loops/* ; do if [ -x $program ] then $program &> /dev/null & fi done # Calculate next_interval next_interval=$(($next_interval % 60000000 + (60000000 / $loops) )) # If minute is not up - call self recursively if [ $next_interval -lt $(( 60000000 / $loops * $loops)) ] then . $0 $loops $next_interval & fi # Otherwise we're done
Launch from cron every minute. Edit your /etc/crontab file and add:
* * * * * root /usr/local/sbin/cron-ms start
Kind of amazing that you can do all this in less than 50 lines of code.