I wrote this very ugly shell script that, at one point, worked perfectly. Now it works for a minute or two and then exits. I know its very ugly but it worked so I never changed it. Could anyone tell me what's going wrong? Any help is greatly appreciated :)

echo "Starting..."
timestamphour=`date +%F_%H`
mkdir /Some/Directory1
mkdir /Some/Directory2
mkdir /Some/Directory3
mkdir /Some/Directory4
mkdir /Some/Directory5
mkdir /Some/Directory6
mkdir /Some/Directory7
mkdir /Some/Directory8
mkdir /Some/Directory9

#This could be done in one step
mkdir /Some/Directory1/$timestamphour
mkdir /Some/Directory2/$timestamphour
mkdir /Some/Directory3/$timestamphour
mkdir /Some/Directory4/$timestamphour
mkdir /Some/Directory5/$timestamphour
mkdir /Some/Directory6/$timestamphour
mkdir /Some/Directory7/$timestamphour
mkdir /Some/Directory8/$timestamphour
mkdir /Some/Directory9/$timestamphour

for i in {1..3600}
#timestampmin=`date +%F_%H-%M`
timestampsec=`date +%F_%H-%M-%S`
sudo curl -o /Some/Directory1/$timestamphour/$timestampsec.jpg http://some.address.at.some:port1/jpg/image.jpg &
sudo curl -o /Some/Directory2/$timestamphour/$timestampsec.jpg http://some.address.at.some:port2/jpg/image.jpg &
sudo curl -o /Some/Directory3/$timestamphour/$timestampsec.jpg http://some.address.at.some:port3/jpg/image.jpg &
sudo curl -o /Some/Directory4/$timestamphour/$timestampsec.jpg http://some.address.at.some:port4/jpg/image.jpg &
sudo curl -o /Some/Directory5/$timestamphour/$timestampsec.jpg http://some.address.at.some:port5/jpg/image.jpg &
sudo curl -o /Some/Directory6/$timestamphour/$timestampsec.jpg http://some.address.at.some:port6/jpg/image.jpg &
sudo curl -o /Some/Directory7/$timestamphour/$timestampsec.jpg http://some.address.at.some:port7/jpg/image.jpg &
sudo curl -o /Some/Directory8/$timestamphour/$timestampsec.jpg http://some.address.at.some:port8/jpg/image.jpg &
sudo curl -o /Some/Directory9/$timestamphour/$timestampsec.jpg http://some.address.at.some:port9/jpg/image.jpg &
sleep 1
  • 3
    If you run it with bash -x does that show you anything of import?
    – roaima
    Dec 4 '15 at 17:41
  • 2
    Is there any message when it exits? Dec 4 '15 at 17:46
  • 3
    you create 32400 (give or take) curl processes with that script? i would look at some kind of sanity check limits - some limit on sockets or background jobs or whatever.
    – mikeserv
    Dec 4 '15 at 18:05
  • 2
    when it stops, run echo $? to get the exit code of whatever died. That may also be helpful, particularly if it's curl. I might also add trap "pgrep curl | wc -l > /tmp/curl-process-count" EXIT; this will drop a count of how many instances of curl are running when the script dies into a small file.
    – DopeGhoti
    Dec 4 '15 at 18:16
  • 1
    @Yllier - there are many ways to narrow it. you need to start with man curl. on the first page you'll find info about wildcards and {brace,patterns}, so you could do http://some.address.some:[0-9]/location/image[0-9].jpg and so on. but you could probably just roll the whole thing into a single curl invocation if you put a little time into learning it.
    – mikeserv
    Dec 7 '15 at 15:30

Try using wait instead of sleep. wait will wait until all background processes finish and then continue, while sleep just delays a second.

See if wait works

  • Did wait work? I'm curious? Please let me know. Dec 4 '15 at 20:42
  • That doesn't seem to have done it. hmm
    – Yllier123
    Dec 4 '15 at 21:01
  • 1
    unvote this answer. it is wrong Dec 4 '15 at 21:03

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.