0

I have the following Script File 'bored.sh'. This file recursively calls itself.

curl "https://www.boredapi.com/api/activity" >>./bored.json
sleep 1s
bash ./bored.sh

I realized that this is a bad approach as it creates several instances and runs many processes that uses the system resources and ultimately crashes the system.

How do I terminate/kill all the previous instances of the scripts after they are executed (series of API call and store those response to file) sequentially but make sure this script runs indefinitely as only one or few child process and create less stress to my system?

EDIT I need to get the data from the API and store it to a file 'bored.json' once in every second.

9
  • This should probably be an infinite loop that keeps calling the same script. while true; do ./bored.sh; done and while you're at it, add a sleep in there too so it's not running all the time (unless that is your goal). Commented Mar 24, 2021 at 16:34
  • True. But how do i kill all the previous parent instances as these are using lot of my system resources. Here is a image of my Task Manager Processes! postimg.cc/5jHyJgGK Commented Mar 24, 2021 at 16:46
  • 1
    You don't seem to understand that your processes never 'complete' as they recursively wait for the next iteration. It's an XY problem which arises from how you choose to run things. This script should only be running once, in a single process. Commented Mar 24, 2021 at 17:04
  • 1
    Please edit your question and start from the beginning. Explain what your script is trying to achieve, and then we can give you a way to do that. Commented Mar 24, 2021 at 17:10
  • 1
    Glad you got it sorted out, feel free to add your approach as an answer down below. Commented Apr 23, 2021 at 8:55

1 Answer 1

0

I was able to figure it out with the help of above the community comments. Instead of recursively calling the file itself as in the above code, I used looping statements to loop through my requirements. The code snippet is below.

while true; do
curl "https://www.boredapi.com/api/activity" >>./bored.json
sleep 1s
./bored.sh; done

I was able to get the expected performance with this one!

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.