LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   run command in every 5 second (https://www.linuxquestions.org/questions/linux-newbie-8/run-command-in-every-5-second-713722/)

ust 03-23-2009 02:47 AM

run command in every 5 second
 
I would like to run a command at every 5 second , I know if I use crontab job to run schedule job , the min. interval is 1 minute , if I want to run at every 5 second ( 12 times / minute ) , what can i do ? thx

ic_torres 03-23-2009 02:54 AM

Quote:

Originally Posted by ust (Post 3484691)
I would like to run a command at every 5 second , I know if I use crontab job to run schedule job , the min. interval is 1 minute , if I want to run at every 5 second ( 12 times / minute ) , what can i do ? thx


i think you can use the while true command..

while true
do
command
sleep 5(seconds)
done



the 5 indicates time in seconds

then you can just save your simple script in a file...

reptiler 03-23-2009 03:02 AM

You could write a shell-script which executes your command and waits for five seconds in an endless loop.
This you can then start using & to have it detach from the TTY. Or run it inside screen if you want to have the possibility to get back to it, like for checking output for example.

Example 1:
Code:

#!/bin/sh
while [ true ]; do
 /usr/local/bin/my_prog
 sleep 5
done

This example will execute the program and then wait for five seconds. The delay between execution thus depends on how long the program actually runs.

Example 2:
Code:

#!/bin/sh
while [ true ]; do
 /usr/local/bin/my_prog &
 sleep 5
done

This one will execute the program in the background and wait for five seconds. Thus it will effectively execute your program every five seconds, no matter how long execution actually takes. But this may cause the program to run multiple times at the same time, if it takes more than 5 seconds to finish whatever it's supposed to do.

Valery Reznic 03-24-2009 03:37 AM

Quote:

Originally Posted by ust (Post 3484691)
I would like to run a command at every 5 second , I know if I use crontab job to run schedule job , the min. interval is 1 minute , if I want to run at every 5 second ( 12 times / minute ) , what can i do ? thx

Executing program every 5 sec while possible doesn't looks like a good idea
Could you explain a reason to do it ?
May be it'll be better to re-write this program as long-living program, that do each 5 sec whatever your like

JaksoDebr 03-24-2009 05:31 PM

In a shell script you probably have to stick with the 'sleep' command. In a compiled program (C/C++ or similar) you have more fine-grained control about timing.

Maybe it is more efficient in terms of system resources to make the code event-based with some simple server-client programming. The Wrox "Linux Programming" has a good example for this.

Linux Archive

mperkel 03-09-2014 06:13 PM

Here's the solution
 
http://wiki.junkemailfilter.com/inde...nds_under_cron

Code:

#! /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



All times are GMT -5. The time now is 06:54 AM.