How to run a Linux script every few seconds under cron
From Computer Tyme Support Wiki
(→Monit Script to make sure cron-ms keeps running) |
(→Monit Script to make sure cron-ms keeps running) |
||
Line 102: | Line 102: | ||
=== Monit Script to make sure cron-ms keeps running === | === Monit Script to make sure cron-ms keeps running === | ||
- | |||
- | |||
<pre> | <pre> | ||
check directory cron-ms path /var/run/cron-ms | check directory cron-ms path /var/run/cron-ms |
Revision as of 21:33, 6 March 2014
Contents |
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:
- Runs as a service - does not need cron
- Launches multiple programs in parallel
- Multiple time periods supported simultaneously just by the directory name
- 2 companion programs to support odd time periods like 37 seconds or 37 minutes
- All standard bash stuff that should run on any *nix systems
- Monit script included to keep it alive
- Simple, elegant, documented, complete, free
- It just works
Even though this software is free if you find it really useful and you want to reward/encourage me you can email me a Amazon Gift Certificate to marc@perkel.com.
Precise Cron with microsecond resolution
The existence of the directory of the number of loops per minute creates the schedule. This version supports microsecond resolution assuming your computer can handle it. The number of executions per minute does not have to to evenly divisible by 60 nor is it limited to 60 I have tested it to 6000 and it works.
This version can be installed in the /etc/init.d directory and run as a service.
#! /bin/sh # chkconfig: 2345 91 61 # description: This program is used to run all programs in a directory in parallel every X times per minute. \ # Think of this program as cron with microseconds resolution. # 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 # The scheduling is done by creating directories with the number of # executions per minute as part of the directory name. # Examples: # /etc/cron-ms/7 # Executes everything in that directory 7 times a minute # /etc/cron-ms/30 # Executes everything in that directory 30 times a minute # /etc/cron-ms/600 # Executes everything in that directory 10 times a second # /etc/cron-ms/2400 # Executes everything in that directory 40 times a second basedir=/etc/cron-ms case "$1" in start|restart|reload) $0 stop mkdir -p /var/run/cron-ms for dir in $basedir/* ; do $0 ${dir##*/} & done exit ;; stop) rm -Rf /var/run/cron-ms exit ;; esac # Loops per minute is passed on the command line loops=$1 interval=$((60000000/$loops)) # Just a heartbeat signal that can be used with monit to verify it's alive touch /var/run/cron-ms # After a restart the PIDs will be different allowing old processes to terminate touch /var/run/cron-ms/$$ # Sleeps until a specific part of a minute with microsecond resolution. 60000000 is full minute usleep $(( $interval - 10#$(date +%S%N) / 1000 % $interval )) # Deleting the PID files exit the program if [ ! -f /var/run/cron-ms/$$ ] then exit fi # Run all the programs in the directory in parallel for program in $basedir/$loops/* ; do if [ -x $program ] then $program &> /dev/null & fi done exec $0 $loops
Kind of amazing that you can do all this in less than 50 lines of code.
Monit Script to make sure cron-ms keeps running
check directory cron-ms path /var/run/cron-ms if timestamp > 1 minute then restart start program = "/etc/init.d/cron-ms start"
Cron-ms touches the /var/run/cron-ms directory every cycle so if the date is older and one minute then cron-ms has stopped running.
Companion Program with cron-min 1 minute resolution
This is a companion program to run a program every X minutes. you can of course do this in crontab but this makes it easier under some circumstances. This program allows you to:
- Set the timing in number of minutes just be the directory name and put as many programs in that directory as you want to start.
- The number of minutes does not need to divide equally into 60.
- The number of minutes isn't limited to 60. You can run a program every 61 minutes or every 923 minutes if you want.
- The number of minutes doesn't have to divide evenly into hours. You can run something every 17 minutes if you want.
#! /bin/sh # Minute Cron # Usage: cron-min 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 # Run this script under Cron or cron-ms once a minute. # This program is used to run all programs in a directory in parallel every X minutes. # The scheduling is done by creating directories with the number of minutes as part of the # directory name. The minutes do not have to evenly divide into 60 or be less than 60. # Examples: # /etc/cron-min/1 # Executes everything in that directory every 1 minute # /etc/cron-min/5 # Executes everything in that directory every 5 minutes # /etc/cron-min/13 # Executes everything in that directory every 13 minutes # /etc/cron-min/75 # Executes everything in that directory every 75 minutes basedir=/etc/cron-min for dir in $basedir/* ; do minutes=${dir##*/} if [ $(( ($(date +%s) / 60) % $minutes )) -eq 0 ] then for program in $basedir/$minutes/* ; do if [ -x $program ] then $program &> /dev/null & fi done fi done
The same thing can be done with 1 second resolution in case you want to run something every 37 seconds or every 90 seconds. You would have to run this script once a second from the above cron-ms program.
# Run this script under cron-ms once a second. # This program is used to run all programs in a directory in parallel every X seconds. # Examples: # /etc/cron-sec/5 # Executes everything in that directory every 5 seconds # /etc/cron-sec/37 # Executes everything in that directory every 37 seconds # /etc/cron-sec/75 # Executes everything in that directory every 75 seconds basedir=/etc/cron-sec for dir in $basedir/* ; do seconds=${dir##*/} if [ $(( $(date +%s) % $seconds )) -eq 0 ] then for program in $basedir/$seconds/* ; do if [ -x $program ] then $program &> /dev/null & fi done fi done
Launching cron-min or cron-sec with Cron or cron-ms
To launch both of the above programs from cron every minute edit your /etc/crontab file and add:
* * * * * root /usr/local/sbin/cron-min
Or you can put this script in the /etc/cron-ms/1 directory and run it under cron-ms. Of you want to run cron-sec you would put it in the /etc/cron-ms/60 directory.