LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 07-01-2012, 06:26 PM   #1
Jajamd
Member
 
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:

Code:
#!/bin/bash
while :
do
curl command1 &
curl command2 &
curl command3
sleep 1
done
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
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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
Jajamd
Member
 
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
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 36
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.

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

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

    # Store the backgrounded curls pid number
    pidx[$foo]=$!
}

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 :
do
    for varx in {1..3}  do
        if  [[ ${pidx[$varx]} -eq 0 ]] || ! kill -0 ${pidx[$varx]} 1>/dev/null 2>&1
        then
            nextcurl $varx
        fi
    done
    sleep 1
done
 
  


Reply

Tags
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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 10:18 AM
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
Only one instance of bash script... cmfarley19 Programming 7 05-06-2003 02:42 PM


All times are GMT -5. The time now is 06:14 AM.

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