Link to home
Start Free TrialLog in
Avatar of aixtutorial
aixtutorialFlag for United States of America

asked on

This is in AIX 5.3..I need a script that needs to run a command every 0.5 seconds

This is in AIX 6.1.and REHL 4.I need a script that needs to run a command every 0.5 secs..Please let me know as how this can be done
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

how long does it take the command to finish? if it is more that 0.5 sec then what is the use?

sleep will give you to wait minimum 1 sec then resume
GNU sleep supports arbitrary floating point numbers (using a period before any fractional digits)


while  true 
do
  <command>
  sleep 0.5
done

Open in new window

As informed by omarfarid
          >  sleep will give you to wait minimum 1 sec then resume


You can use the following:
cat sleep_halfSec.c
#include <unistd.h>
#include <time.h>
/* Sleep for milliseconds */
void sleepMilli(int millisec)
{
        struct timespec tmsp;
        tmsp.tv_sec = millisec / 1000;
        tmsp.tv_nsec = (millisec % 1000) * 1000;
        nanosleep(&tmsp, NULL);
}
int main()
{
        sleepMilli( 500) ; /* which is 1000*0.5 */
        return 0 ;
}

Open in new window



Compile that code
cc -g  sleep_halfSec.c -o sleep_halfSec

Open in new window



Here goes the script which you need:

cat sleep_halfSec.sh
#!/bin/ksh
while true
do
        echo Execute your required command
        echo sleep for 0.5 second
        sleep_halfSec
        echo Execute your required command
done

Open in new window

or
cat sleep_halfSec.sh
#!/bin/ksh
while [ 1 ]
do
        echo Execute your required command
        echo sleep for 0.5 second
        sleep_halfSec
        echo Execute your required commands
done

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial