How to run a Linux script every few seconds under cron

From Computer Tyme Support Wiki

(Difference between revisions)
Jump to: navigation, search
(Created page with "Did you ever want to run a program every few seconds under a linux, unix, bsd or osx cron script? Here's an ellegant script that does just that. You can get 2,3,4,5,6,10,12,15,20...")
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 ellegant script that does just that. You can get 2,3,4,5,6,10,12,15,20,30 second resolution.
Did you ever want to run a program every few seconds under a linux, unix, bsd or osx cron script? Here's an ellegant script that does just that. You can get 2,3,4,5,6,10,12,15,20,30 second resolution.
 +
 +
#! /bin/sh
 +
 +
# Run all programs in a directory in parallel
 +
# Usage: run-parallel directory delay
 +
# Copyright 2013 by Marc Perkel - http://www.junkemailfilter.com
 +
# Free to use with attribution
 +
 +
if [ $# -eq 0 ]
 +
then
 +
    echo
 +
    echo "run-parallel by Marc Perkel - support@junkemailfilter.com"
 +
    echo "free to copy and use with attribution"
 +
    echo
 +
    echo "Usage: run-parallel [directory] [delay]"
 +
    echo
 +
    echo "Example:"
 +
    echo "  run-parallel /etc/cron.20sec 20"
 +
    echo "  Runs all executable files in /etc/cron.20sec every 20 seconds."
 +
    echo
 +
    echo "If delay parameter is missing it runs everything once and exits."
 +
    echo
 +
    echo "You could make a script containing the following and run it once a minute from cron for 2,3,4,5,6,10,12,15,20,30 second resolution."
 +
    echo
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.2sec 2 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.3sec 3 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.4sec 4 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.5sec 5 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.6sec 6 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.10sec 10 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.12sec 12 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.15sec 15 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.20sec 20 &"
 +
    echo  "/usr/local/sbin/run-parallel /etc/cron.30sec 30 &"
 +
    echo
 +
    exit
 +
fi
 +
 +
dir=$1
 +
delay=$2
 +
 +
# Exit if directory doesn't exist
 +
 +
if ! [ -d $dir ]
 +
then
 +
    exit
 +
fi
 +
 +
# Exit if directory has no files
 +
 +
if ! [ "$(ls -A $dir/)" ]
 +
then
 +
    exit
 +
fi
 +
 +
# Sleep if both $delay and $counter are set
 +
 +
if ! [ -z $delay ]
 +
then
 +
    if ! [ -z $counter ]
 +
    then
 +
      sleep $delay
 +
    fi
 +
fi
 +
 +
# Set counter to 0 if not set
 +
 +
if [ -z $counter ]
 +
then
 +
    counter=0
 +
fi
 +
 +
# Run all the programs in the directory in parallel
 +
 +
for program in $dir/* ; do
 +
if [ -x $program ]
 +
    then
 +
      $program > /dev/null 2> /dev/null &
 +
    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

Revision as of 19:52, 8 January 2014

Did you ever want to run a program every few seconds under a linux, unix, bsd or osx cron script? Here's an ellegant script that does just that. You can get 2,3,4,5,6,10,12,15,20,30 second resolution.

#! /bin/sh

# Run all programs in a directory in parallel
# Usage: run-parallel directory delay
# Copyright 2013 by Marc Perkel - http://www.junkemailfilter.com
# Free to use with attribution

if [ $# -eq 0 ]
then
   echo
   echo "run-parallel by Marc Perkel - support@junkemailfilter.com"
   echo "free to copy and use with attribution"
   echo
   echo "Usage: run-parallel [directory] [delay]"
   echo
   echo "Example:"
   echo "   run-parallel /etc/cron.20sec 20"
   echo "   Runs all executable files in /etc/cron.20sec every 20 seconds."
   echo 
   echo "If delay parameter is missing it runs everything once and exits."
   echo
   echo "You could make a script containing the following and run it once a minute from cron for 2,3,4,5,6,10,12,15,20,30 second resolution."
   echo
   echo  "/usr/local/sbin/run-parallel /etc/cron.2sec 2 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.3sec 3 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.4sec 4 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.5sec 5 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.6sec 6 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.10sec 10 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.12sec 12 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.15sec 15 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.20sec 20 &"
   echo  "/usr/local/sbin/run-parallel /etc/cron.30sec 30 &"
   echo
   exit
fi

dir=$1
delay=$2

# Exit if directory doesn't exist

if ! [ -d $dir ]
then
   exit
fi

# Exit if directory has no files

if ! [ "$(ls -A $dir/)" ]
then
   exit
fi

# Sleep if both $delay and $counter are set

if ! [ -z $delay ]
then
   if ! [ -z $counter ]
   then
      sleep $delay
   fi
fi

# Set counter to 0 if not set

if [ -z $counter ]
then
   counter=0
fi

# Run all the programs in the directory in parallel

for program in $dir/* ; do
if [ -x $program ] 
   then
      $program > /dev/null 2> /dev/null &
   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
Personal tools