1

I want to repeatedly run multiple commands at a time interval using a script. I tried this

----------------test_script--------------- while true;do ls -l >>output.txt sleep 3 done while true;do cat file.txt sleep 5 done 

i want to run both while loops at same time .When i run the above script ,only first while loop is running and the output of ls -l is redirected to the file .How i can execute both while loops simultaneously from the script

5
  • 1
    You are anyway running infinite loops. So why not put both ls and cat in the same while. Commented Jul 15, 2016 at 9:56
  • 1
    Use & to run a command in background. Commented Jul 15, 2016 at 10:01
  • @choroba : But this is a bit involved than that. Perhaps I was looking at one of your earlier [ solution ]. Commented Jul 15, 2016 at 10:05
  • 1
    @ Fazlin my intention was to run both commands at specified time intervel repeatedly. If running from same while loop i have to consider other delays also ,if a am running 5 commands like this ,the first command will run for the second time after going through all the other 4 commands. Commented Jul 15, 2016 at 10:07
  • @Adhz: Please provide your suggestions/observations on the provided answer(s), so that it will be useful to help you out further! Commented Jul 15, 2016 at 10:51

1 Answer 1

1

One way to do is run one of the loops in the background and other in the fore like below.

#!/bin/bash while true;do ls -l >>output.txt sleep 3 done & # Runs the first while loop in the background and passes to the next while loop while true;do cat file.txt sleep 5 done 
Sign up to request clarification or add additional context in comments.

1 Comment

It would be more efficient to redirect to output.txt only once, after the done. Then you can use an overwriting rediect if you like, to clear any previous contents in the file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.