HOWTO: August 2004 Archives

Pausing in shell scripts

| | Comments (1)
Sometimes in the middle of a shell script you may need to have a command pause for a few seconds, minutes or hours. Although there are some simple programs you can download to help you with this, few realize that you can also just use the ping command to create a delay.

Within a DOS batch file you can execute the following:
ping -n 1 -w 1000 1.1.1.1 >NUL
By modifying the -w parameter value, you can specify the amount of time to wait for a reply in milliseconds. The above will wait for 1 second. You do need to make sure the address you are trying to ping is unreachable, otherwise the command will finish quicker.

The UNIX equivalent is the following:
ping -c 2 -n -i 1 127.0.0.1 >/dev/null
You don't get millisecond precision with the UNIX version of ping. Modify the -i parameter to specify the delay in seconds. This method of creating a delay doesn't rely on a non-reachable address, instead you are setting ping to send 2 packets to your local machine and to wait for -i seconds in between.

Nifty, eh? I used these methods to upload a webcam image to a server once a minute from both a Windows and UNIX machine.

Update: Alright, I've already been informed twice of the existence of the unix command sleep. Just between you and me though, the cool people use ping.

About this Archive

This page is a archive of entries in the HOWTO category from August 2004.

HOWTO: May 2004 is the previous archive.

HOWTO: August 2006 is the next archive.

Find recent content on the main index or look in the archives to find all content.