Unix Shell Script: sleep command not working

10,549

Solution 1

Make sure your text editor is not putting a /r /n and only a /n for every new line. This is typical if you are writing the script on windows.

Use notepad++ (windows) and go to edit|EOL convention|UNIX then save it. If you are stuck with it on the computer, i have read from here [talk.maemo.org/showthread.php?t=67836] that you can use [tr -d "\r" < oldname.sh > newname.sh] to remove the problem. if you want to see if it has the problem use [od -c yourscript.sh] and /r will occur before any /n.

Other problems I have seen it cause is cd /dir/dir and you get [cd: 1: can't cd to /dir/dir] or copy scriptfile.sh newfilename the resulting file will be called newfilenameX where X is an invisible character (ensure you can delete it before trying it), if the file is on a network share, a windows machine can see the character. Ensure it is not the last line for a successful test.

Until i figured it out (i knew i had to ask google for something that may manifest in various ways) i thought that there was an issue with this linux version i was using (sleep not working in a script???).

Solution 2

Are you sure you are using sleep the right way? Based on your description, you should be invoking it as:

sleep 180

Is this the way you are doing it?

You might also want to consider wget command as it has an explicit --wait flag, so you might avoid having the loop in the first place.

Solution 3

while read -r urlname
do
    curl ..........${urlname}....
    sleep 180  #180 seconds is 3 minutes
done  <   file_with_several_url_to_be_fetched

?

Share:
10,549
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin about 2 years

    i have a scenario in which i need to download files through curl command and want my script to pause for some time before downloading the second one. I used sleep command like sleep 3m but it is not working. any idea ???

    thanks in advance.

  • Basher51
    Basher51 over 9 years
    You made my day.Was stuck with this issue for a few days.I was not able to execute commands which I edited in notepad++ on windows.When I chose the proper convention things worked.Many thanks !
  • nmu
    nmu about 3 years
    Gosh thank you so much! My script was failing in a bunch of non obvious ways and it just didn't hit me that this is what was occuring. Honestly thank you so much. I would not have figured it out if not for this answer.