Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-01-2012, 06:26 PM   #1
Registered: Aug 2004
Posts: 37

Rep: Reputation: 0
Bash script: restart each instance after it has ended within a loop

Hello everyone,

I'm writing a little script to have several instances of curl download different things at the same time. This is what I have so far:

while :
curl command1 &
curl command2 &
curl command3
sleep 1
The three instances of curl are running at the same time. This is what I wanted.
But all of them are only restarted after the last instance of curl (curl command 3) has finished. I need to find a way to restart each instance of curl after it has finished instead of having to wait for the last one to finish.

I hope it makes sense !

Thank you very much for your help.
Old 07-01-2012, 06:34 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I'd take it one command at a time---for example, what is supposed to happen after "curl command1" finishes? If it's just supposed to run again, why not put it inside its own loop?

BUT, why would you want to keep running the same curl command repeatedly? On a lot of sites, if you do that, you'll probably be kicked off rather quickly.

In the global sense, what are you trying to accomplish?
2 members found this post helpful.
Old 07-01-2012, 06:38 PM   #3
Registered: Aug 2004
Posts: 37

Original Poster
Rep: Reputation: 0
It's not the same curl command repeatedly. I'm adding a variable each time.
I'm going to follow your suggestion and put every command inside its own loop. Thanks for the tip
Old 07-01-2012, 09:15 PM   #4
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
I am sure there is more than one way to do this. But I've put some pseudo-code down here, as to how it might be done.

Basically, I've decided to store the pid of each backgrouded curl command in an array, and to periodically poll each process pid with a kill -0 command, to see if it still running. And if not, then the nextcurl() function will be called, to start up the next curl command.

Furthermore, I suspect that backgrounded processes do not always die when their parent is killed. So a trap function may be necessary here too.

Its all quite rough and untested, bad-habits and errors in there, no doubt.

# Run next curl command. $1 = the job number
nextcurl()  {

    # Generate and run your next curl command. Pseudo-code. 
    curl command1 1>/dev/null 2>&1 &    # disown

    # Store the backgrounded curls pid number

pidx=( 0 0 0 )

# Some kind of trap function may be needed here, to ensure that
# all children processes are killed at exit.

trap ' for numx in "${pidx[$@]}" ; do kill $numx ; done ; exit ; ' INT

# The pids of running curls are each tested, to see if they are still running. If not,
# then the nextcurl function is called, to restart the next curl command

while :
    for varx in {1..3}  do
        if  [[ ${pidx[$varx]} -eq 0 ]] || ! kill -0 ${pidx[$varx]} 1>/dev/null 2>&1
            nextcurl $varx
    sleep 1


bash, curl, loop

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Only one instance of bash script... cmfarley19 Programming 11 01-13-2018 05:41 AM
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 04:49 PM
Single Instance of Bash Script binbash Programming 19 10-06-2010 09:37 AM
bash script loop bong.mau Programming 6 09-14-2005 08:38 PM
bash script for loop drisay Programming 5 12-25-2004 01:32 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration