How to run a Linux script every few seconds under cron

From Computer Tyme Support Wiki

(Difference between revisions)
Jump to: navigation, search
(Simple Example)
(Overview)
 
(87 intermediate revisions not shown)
Line 1: Line 1:
-
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. You can get 1,2,3,4,5,6,10,12,15,20,30 second resolution. Just run it once a minute under crond and it just works.
+
= 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 bash script that does just that.  
Features:
Features:
-
* Run once per minute under cron
+
* It can run stand alone or as a service - does not need 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
-
* Use of timeout kills programs that don't terminate within delay time
+
* 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
* It just works
-
* Simple, elegant, free
 
-
I'm now posting two versions of the program. The first version is simpler and is good for running as many as 30 times a minute. There are 2 issues however. crond as it turns out isn't real precise and it starts programs 1 to 1.5 seconds after the minute. Also - since the program call itself over and over during the minute there is some drift. There's enough drift so you can't really use it to run something every second. But every 2 seconds or greater is fine.
+
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.
-
= Simple Example =
+
== 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.
-
<pre>
+
This version can be installed in the /etc/init.d directory and run as a service.
-
#! /bin/sh
+
-
+
-
# Run all programs in a directory in parallel
+
-
# Usage: run-parallel directory delay
+
-
# Copyright 2013 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
+
-
+
-
if [ $# -eq 0 ]
+
-
then
+
-
    echo
+
-
    echo "run-parallel by Marc Perkel"
+
-
    echo
+
-
    echo "This program is used to run all programs in a directory in parallel"
+
-
    echo "or to rerun them every X seconds for one minute."
+
-
    echo "Think of this program as cron with seconds resolution."
+
-
    echo
+
-
    echo "Usage: run-parallel [directory] [delay]"
+
-
    echo
+
-
    echo "Examples:"
+
-
    echo "  run-parallel /etc/cron.20sec 20"
+
-
    echo "  run-parallel 20"
+
-
    echo "  # Runs all executable files in /etc/cron.20sec every 20 seconds or 3 times a minute."
+
-
    echo
+
-
    echo "If delay parameter is missing it runs everything once and exits."
+
-
    echo "If only delay is passed then the directory /etc/cron.[delay]sec is assumed."
+
-
    echo
+
-
    echo 'If "cronsec" is passed then it runs all of these delays 2 3 4 5 6 10 12 15 20 30'
+
-
    echo "resulting in 30 20 15 12 10 6 5 4 3 2 executions per minute."
+
-
    echo
+
-
    exit
+
-
fi
+
-
+
-
# If "cronsec" is passed as a parameter then run all the delays in parallel
+
-
+
-
if [ $1 = cronsec ]
+
-
then
+
-
    for interval in 2 3 4 5 6 10 12 15 20 30
+
-
    do
+
-
      $0 $interval &
+
-
    done
+
-
    exit
+
-
fi
+
-
+
-
# Set the directory to first parameter and delay to second parameter
+
-
+
-
dir=$1
+
-
delay=$2
+
-
+
-
# If only parameter is 2,3,4,5,6,10,12,15,20,30 then automatically calculate
+
-
# the standard directory name /etc/cron.[delay]sec
+
-
+
-
if [[ "$1" =~ ^(2|3|4|5|6|10|12|15|20|30)$ ]]
+
-
then
+
-
    dir="/etc/cron.$1sec"
+
-
    delay=$1
+
-
fi
+
-
+
-
# Exit if directory doesn't exist or has no files
+
-
+
-
if [ ! "$(ls -A $dir/ 2> /dev/null)" ]
+
-
then
+
-
    exit
+
-
fi
+
-
 
+
-
# Sleep if $counter is set
+
-
+
-
if [ ! -z $counter ]
+
-
then
+
-
    sleep $delay
+
-
fi
+
-
+
-
# Run all the programs in the directory in parallel
+
-
# Use of timeout ensures that the processes are killed if they run too long
+
-
+
-
for program in $dir/* ; do
+
-
    if [ -x $program ]
+
-
    then
+
-
      if [ 0$delay -gt 0 ]
+
-
      then
+
-
          timeout $delay $program &> /dev/null &
+
-
      else
+
-
          $program &> /dev/null &
+
-
      fi
+
-
    fi
+
-
done
+
-
+
-
# If delay not set then we're done
+
-
+
-
if [ -z $delay ]
+
-
then
+
-
    exit
+
-
fi
+
-
+
-
# Add delay to counter
+
-
+
-
counter=$(( $counter + $delay ))
+
-
+
-
# If minute is not up - call self recursively
+
-
+
-
if [ $counter -lt 60 ]
+
-
then
+
-
    . $0 $dir $delay &
+
-
fi
+
-
+
-
# Otherwise we're done
+
-
</pre>
+
-
You can then create the directories needed to put your programs in that you want to run. You don't have to create the directories that you aren't going to use.
+
chkconfig cron-ms on
 +
service cron-ms start
-
mkdir /etc/cron.2sec
+
You don't need to run it as a service. You can just copy it into /usr/bin and put it in a startup script somewhere.
-
mkdir /etc/cron.3sec
+
-
mkdir /etc/cron.4sec
+
-
mkdir /etc/cron.5sec
+
-
mkdir /etc/cron.6sec
+
-
mkdir /etc/cron.10sec
+
-
mkdir /etc/cron.12sec
+
-
mkdir /etc/cron.15sec
+
-
mkdir /etc/cron.20sec
+
-
mkdir /etc/cron.30sec
+
-
To run every minute you can edit your /etc/crontab file and add:
+
You'll also need usleep. If you can't find usleep in your Linux distribution install busybox and run this:
-
  * * * * * root /usr/local/sbin/run-parallel cronsec
+
  ln /bin/busybox /bin/usleep
-
Keep in mind that the programs that you are trying need to be written to complete in the allotted time or they will be killed before completing. Also keep in mind that on a heavily loaded system that on smaller intervals there could be some time drift over the 1 minute period with unknown results. This is especially true of the 1 second interval.
+
Here's the program. It's just a simple bash script.
-
 
+
-
Have a comment? You can email me at support@junkemailfilter.com
+
-
 
+
-
= More Precise Implementation =
+
-
 
+
-
Maybe I'm too picky but I want it to start right on the minute and have the intervals not be subject to drift/loading errors. So here is a slightly more complex version. The only drawback to this version is that it will take at least a full minute to start and maybe up to 2 minutes. That's because when it is launched it waits out the first minute to start processes right at the top of the minute instead of being late.
+
<pre>
<pre>
#! /bin/sh
#! /bin/sh
-
# Run all programs in a directory in parallel
+
# chkconfig: 2345 91 61
-
# Usage: run-parallel directory delay
+
# description: This program is used to run all programs in a directory in parallel every X times per minute. \
-
# Copyright 2013 by Marc Perkel
+
#              Think of this program as cron with microseconds resolution.
-
# docs at http://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_every_few_seconds_under_cron"
+
 
 +
# Microsecond Cron
 +
# Usage: cron-ms start
 +
# Copyright 2014 by Marc Perkel - marc@perkel.com
 +
# docs at http://wiki.junkemailfilter.com/index.php/How_to_run_a_Linux_script_every_few_seconds_under_cron
# Free to use with attribution
# 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
if [ $# -eq 0 ]
if [ $# -eq 0 ]
then
then
-
  echo
+
   echo "Usage $0 start|stop"
-
  echo "run-parallel by Marc Perkel"
+
-
  echo
+
-
  echo "This program is used to run all programs in a directory in parallel"
+
-
  echo "or to rerun them every X seconds for one minute."
+
-
  echo "Think of this program as cron with seconds resolution."
+
-
  echo
+
-
   echo "Usage: run-parallel [directory] [delay]"
+
-
  echo
+
-
  echo "Examples:"
+
-
  echo "  run-parallel /etc/cron.20sec 20"
+
-
  echo "  run-parallel 20"
+
-
  echo "  # Runs all executable files in /etc/cron.20sec every 20 seconds or 3 times a minute."
+
-
  echo
+
-
  echo "If delay parameter is missing it runs everything once and exits."
+
-
  echo "If only delay is passed then the directory /etc/cron.[delay]sec is assumed."
+
-
  echo
+
-
  echo 'If "cronsec" is passed then it runs all of these delays 1 2 3 4 5 6 10 12 15 20 30'
+
-
  echo "resulting in 60 30 20 15 12 10 6 5 4 3 2 executions per minute."  
+
-
  echo
+
   exit
   exit
fi
fi
-
# Runs like the sleep command but compensates for drift to start on time
+
case "$1" in
-
function syncsleep ()
+
  start|restart|reload)
-
{
+
  $0 stop
-
   usleep $(( $1000000 - (10#$(date +%N) / 1000) ))
+
   mkdir -p /var/run/cron-ms
-
}
+
-
 
+
-
# If "cronsec" is passed as a parameter then run all the delays in parallel
+
-
 
+
-
if [ $1 = cronsec ]
+
-
then
+
-
   # Wait till start of next minute
+
   # Here I'm adding some extra directories to the $PATH.
-
   syncsleep $(( 60 - 10#$(date +%S) ))
+
   PATH=/usr/local/sbin:/usr/local/bin:$PATH
 +
  export PATH
-
   for interval in 1 2 3 4 5 6 10 12 15 20 30
+
   for dir in /etc/cron-ms/* ; do
-
  do
+
       $0 ${dir##*/} &
-
       $0 $interval &
+
   done
   done
   exit
   exit
-
fi
+
  ;;
-
# Set the directory to first parameter and delay to second parameter
+
  stop)
 +
  rm -Rf /var/run/cron-ms
 +
  exit
 +
  ;;
-
dir=$1
+
esac
-
delay=$2
+
-
# If only parameter is 1,2,3,4,5,6,10,12,15,20,30 then automatically calculate
+
# Loops per minute is passed on the command line as $1
-
# the standard directory name /etc/cron.[delay]sec
+
-
if [[ "$1" =~ ^(1|2|3|4|5|6|10|12|15|20|30)$ ]]
+
microseconds=$((60000000/$1))
-
then
+
-
  dir="/etc/cron.$1sec"
+
-
  delay=$1
+
-
fi
+
-
# Exit if directory doesn't exist or has no files
+
# After a restart the PIDs will be different allowing old processes to terminate
 +
# Touching /var/run/cron-ms is a heartbeat signal that can be used with monit to verify it's alive
-
if [ ! "$(ls -A $dir/ 2> /dev/null)" ]
+
touch /var/run/cron-ms /var/run/cron-ms/$$
-
then
+
-
  exit
+
-
fi
+
-
# Sleep if $counter is set
+
# Sleeps until a specific part of a minute with microsecond resolution. 60000000 is full minute
-
if [ ! -z $counter ]
+
usleep $(( $microseconds - $(date +%s%N) / 1000 % $microseconds ))
 +
 
 +
# Deleting the PID files exit the program
 +
 
 +
if [ ! -f /var/run/cron-ms/$$ ]
then
then
-
   syncsleep $delay
+
   exit
fi
fi
# Run all the programs in the directory in parallel
# Run all the programs in the directory in parallel
-
# Use of timeout ensures that the processes are killed if they run too long
 
-
for program in $dir/* ; do
+
for program in /etc/cron-ms/$1/* ; do
-
   if [ -x $program ]
+
   $program &> /dev/null &
-
  then
+
-
      if [ 0$delay -gt 0 ]
+
-
      then
+
-
        timeout $delay $program &> /dev/null &
+
-
      else
+
-
        $program &> /dev/null &
+
-
      fi
+
-
  fi
+
done
done
-
# If delay not set then we're done
+
exec $0 $1
 +
</pre>
-
if [ -z $delay ]
+
Kind of amazing that you can do all this in less than 50 lines of code.
-
then
+
-
  exit
+
-
fi
+
-
# Add delay to counter
+
You can add or remove programs from the /etc/cron-ms directories while cron-ms is running. However if create or remove directories you will need to restart the program.
-
counter=$(( $counter + $delay ))
+
=== Monit Script to make sure cron-ms keeps running ===
 +
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 and monit can restart it.
-
# If minute is not up - call self recursively
+
<pre>
 +
# /var/run/cron-ms is touched every cycle as heartbeat signal
-
if [ $counter -lt 60 ]
+
check directory cron-ms-run path /var/run/cron-ms
-
then
+
if does not exist then exec "/etc/init.d/cron-ms restart"
-
  . $0 $dir $delay &
+
if timestamp > 1 minute then exec "/etc/init.d/cron-ms restart"
-
fi
+
-
# Otherwise we're done
+
# if a directory is created or deleted in /etc/cron-ms then restart
 +
check directory cron-ms-etc path /etc/cron-ms
 +
if timestamp < 1 minute then exec "/etc/init.d/cron-ms restart"
</pre>
</pre>
 +
 +
== Companion Program cron-min with 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 - marc@perkel.com
 +
# 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
 +
          $program &> /dev/null &
 +
      done
 +
    fi
 +
done
 +
 +
== Companion Program cron-sec with 1 second resolution ==
 +
 +
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. The only new feature this script adds is the ability to use intervals greater than 60 seconds.
 +
 +
#! /bin/sh
 +
 +
# 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/37    # Executes everything in that directory every 37 seconds
 +
#  /etc/cron-sec/75    # Executes everything in that directory every 75 seconds
 +
#  /etc/cron-sec/987    # Executes everything in that directory every 987 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
 +
          $program &> /dev/null &
 +
      done
 +
    fi
 +
done
 +
 +
== Companion Program run-parallel to execute all programs in a different directory ==
 +
 +
If you have another directory where you have programs that you want to run you can use this script to run all of them in parallel.
 +
 +
#! /bin/sh
 +
 +
# Runs all programs in a directory in parallel
 +
# Usage /usr/local/sbin/run-parallel <dir>
 +
 +
for program in $1/* ; do
 +
    $program &> /dev/null &
 +
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.

Latest revision as of 22:20, 16 November 2017

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 bash script that does just that.

Features:

  • It can run stand alone or 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.

chkconfig cron-ms on
service cron-ms start

You don't need to run it as a service. You can just copy it into /usr/bin and put it in a startup script somewhere.

You'll also need usleep. If you can't find usleep in your Linux distribution install busybox and run this:

ln /bin/busybox /bin/usleep

Here's the program. It's just a simple bash script.

#! /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 - marc@perkel.com
# 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

if [ $# -eq 0 ]
then
   echo "Usage $0 start|stop"
   exit
fi

case "$1" in

   start|restart|reload)
   $0 stop
   mkdir -p /var/run/cron-ms

   # Here I'm adding some extra directories to the $PATH.

   PATH=/usr/local/sbin:/usr/local/bin:$PATH
   export PATH

   for dir in /etc/cron-ms/* ; do
      $0 ${dir##*/} &
   done

   exit
   ;;

   stop)
   rm -Rf /var/run/cron-ms
   exit
   ;;

esac

# Loops per minute is passed on the command line as $1

microseconds=$((60000000/$1))

# After a restart the PIDs will be different allowing old processes to terminate
# Touching /var/run/cron-ms is a heartbeat signal that can be used with monit to verify it's alive

touch /var/run/cron-ms /var/run/cron-ms/$$

# Sleeps until a specific part of a minute with microsecond resolution. 60000000 is full minute

usleep $(( $microseconds - $(date +%s%N) / 1000 % $microseconds ))

# 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 /etc/cron-ms/$1/* ; do
   $program &> /dev/null &
done

exec $0 $1

Kind of amazing that you can do all this in less than 50 lines of code.

You can add or remove programs from the /etc/cron-ms directories while cron-ms is running. However if create or remove directories you will need to restart the program.

Monit Script to make sure cron-ms keeps running

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 and monit can restart it.

# /var/run/cron-ms is touched every cycle as heartbeat signal

check directory cron-ms-run path /var/run/cron-ms
if does not exist then exec "/etc/init.d/cron-ms restart"
if timestamp > 1 minute then exec "/etc/init.d/cron-ms restart"

# if a directory is created or deleted in /etc/cron-ms then restart

check directory cron-ms-etc path /etc/cron-ms
if timestamp < 1 minute then exec "/etc/init.d/cron-ms restart"

Companion Program cron-min with 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 - marc@perkel.com
# 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
         $program &> /dev/null &
      done
   fi
done

Companion Program cron-sec with 1 second resolution

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. The only new feature this script adds is the ability to use intervals greater than 60 seconds.

#! /bin/sh

# 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/37     # Executes everything in that directory every 37 seconds
#  /etc/cron-sec/75     # Executes everything in that directory every 75 seconds
#  /etc/cron-sec/987    # Executes everything in that directory every 987 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
         $program &> /dev/null &
      done
   fi
done

Companion Program run-parallel to execute all programs in a different directory

If you have another directory where you have programs that you want to run you can use this script to run all of them in parallel.

#! /bin/sh

# Runs all programs in a directory in parallel
# Usage /usr/local/sbin/run-parallel <dir>

for program in $1/* ; do
   $program &> /dev/null &
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.

Personal tools