i have a while loop to act as stopwatch and i want to implment it in script to count the time taken by command to run and i want to kill it after command finish

date1=`date +%s`; while true; do 
   echo -ne "$(date -u --date @$((`date +%s` - $date1)) +%H:%M:%S)\r";
done &

i need to kill this loop inside the script

i tried

    1-jobs but it gives me no output
    2-kill $! but give me erro

but the loop is keep running

  • What about date1=<date ..> ; command & wait ; echo -ne <date_diff>? – Fiximan Aug 22 '16 at 14:41
  • its full script with multiple command and i need to implement this part of loop to count time taken by each part of script in real time – Khalid Abo El MaGd Aug 22 '16 at 14:44

Use the timeout command provided by the coreutils package.


   timeout - run a command with a time limit

   timeout [OPTION]

   Start COMMAND, and kill it if still running after DURATION.
  • that's not what i want ..i need to get the script running time – Khalid Abo El MaGd Aug 22 '16 at 16:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.