LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-23-2009, 03:47 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
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
 
Old 03-23-2009, 03:54 AM   #2
ic_torres
Member
 
Registered: Nov 2005
Location: ABAP
Distribution: slackware 12.0, Vector Linux STD 6.0 and 5.8, ZenWalk 4.6.1, OpenBSD 3.9
Posts: 389

Rep: Reputation: 34
Quote:
Originally Posted by ust View Post
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...
 
Old 03-23-2009, 04:02 AM   #3
reptiler
Member
 
Registered: Mar 2009
Location: Hong Kong
Distribution: Fedora
Posts: 184

Rep: Reputation: 41
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.
 
Old 03-24-2009, 04:37 AM   #4
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 675

Rep: Reputation: 136Reputation: 136
Quote:
Originally Posted by ust View Post
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
 
Old 03-24-2009, 06:31 PM   #5
JaksoDebr
Member
 
Registered: Mar 2009
Distribution: Fedora, Slackware
Posts: 104

Rep: Reputation: 21
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

Last edited by JaksoDebr; 04-02-2009 at 06:13 AM.
 
Old 03-09-2014, 07:13 PM   #6
mperkel
LQ Newbie
 
Registered: Oct 2005
Posts: 15

Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
trying to fix xwindows or just run command line... changing run levels dave247 Debian 2 11-18-2008 07:11 PM
Shell: command not found / Runs fine with "Run command" badbunny Linux - Newbie 1 01-22-2007 02:21 AM
Command to run another command against a list of files psweetma Linux - General 3 11-09-2005 06:29 PM
Cannot run CP command! hrudolph Linux - Newbie 12 05-14-2005 11:52 PM
Run Command csosa Linux - Software 1 09-27-2003 11:41 AM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration