Repeat a command for one sec

How to repeat the execution of a simple command like the following for 1 sec ?
echo Hi

The completion time for the command is not known, but we need to calculate the number of times this commans executes successfully within 1 sec.

Thanks
Kumarjit

Fork a thread and kill the thread after sleeping for 1 sec in the parent, in the provided example the command produces measurable contents that allow the number of runs to be counted.

This sounds to homework-esque to provide implementation details.

@Skrynesaver : Sounds latin n greek to me !!!!! :confused:
I dont really have any significant knowledge about FORKS
and THREADS .
Are there any reference manuals that may be helpful in this regard ?

Thanks
Kumarjit.

I interpret your question this way - You have a need to execute a command continuously for X seconds and find out how many times the command runs successfuly within that period of X seconds. And you have X as 1 now.

Below is one approach but consider the execution time of these extra commands - date, if $? -eq 0 - which may consume some extra time if you want to measure the performance of your command.

CURR_SECS=$(date +%s)
while $CURR_SECS -lt $CURR_SECS + 1
do
  echo Hi
  if [ $? -eq 0 ]
  then
   SUCCESS_COUNT=$(expr $SUCCESS_COUNT + 1)
  fi
  CURR_SECS=$(date +%s)
done

Note: This is not tested and might have syntax errors.

I believe there are few changes needed. This will definitely get into a infinite loop due to highlighted lines :slight_smile:

May be you have to set a variable for the next second :wink:

1 Like

Yes, I meant to store

$CURR_SECS + 1

in a variable. Thanks for pointing out.