Alternative to Sleep?

Greetings.

I've been wondering about this one for some time: Is there an alternative to sleep in bash?

The reason: I'd like to simply limit the amount of processor usage in continuous while : script scenarios without spawning endless sleep processes as well. After beating the manpages, I still haven't a clue as to how one might do something like this...

Any ideas? Is it even possible???

Running 'Buntu 12.04, BTW :wink:

Thanks!

Why is

a problem. You could try using the

subsystem or

.

Switch to ksh:

$ ksh
$ whence -v sleep
sleep is a shell builtin
$

Or maybe this...

#! /bin/bash
mkfifo myfifo 2>/dev/null
n=0
cat myfifo | while : ; do
        ((n=n+1))
        read -t 2 junk
        echo loop $n
done

:smiley:

1 Like

As used in the AudioScope.sh project:-

delay()
{
	read -n1 -s -t$1
}

Called as delay <time_in_seconds> and has the advantage of keyboard override...

However, it requires a tty. Not gonna fly in a cron job or even a background process.

1 Like

Thanks for the feedback, folks.

However, "read" will not work from an actual script context in Xubuntu 12.04. The "while" simply blows past it; and this comes out every time:

"illegal option -t"

:mad:

Of course, it'll run just fine directly from the terminal or in any context on Parted Magic...

Anything else to play with out there which has a timeout that might be usable???

Thanks Again!

I guess you have an older bash then. Your only options are to switch to a language with more power or just live with the sleep processes. The sleep processes would not bother me and I don't know why you worry about them.

So ksh, newer bash, or something like perl would all do what you want.

Bit of lateral thinking here.
How about using the sound card as your delay; ensure it is enabled at maximum volume.
Some demo code to show as an example, OSX 10.7.5 default bash terminal, just
comment out the "afplay" lines and uncomment the "aplay" lines for Ubuntu:-

#!/bin/bash --posix
# timer
# *********************************************************
# Generate a 1 second delay and beep.
# Issued as Public Domain, CC0. You may do with it as you please.
# *********************************************************
if [ ! -e "/tmp/sinewave.wav" ]
then
	> /tmp/sinewave.wav
	printf "\x52\x49\x46\x46\x64\x1F\x00\x00\x57\x41\x56\x45\x66\x6D\x74\x20\x10\x00\x00\x00\x01\x00\x01\x00\x40\x1F\x00\x00\x40\x1F\x00\x00\x01\x00\x08\x00\x64\x61\x74\x61\x40\x1F\x00\x00" >> /tmp/sinewave.wav
	cp /tmp/sinewave.wav /tmp/timer.wav
	for n in {0..999}
	do
		printf "\x80\x26\x00\x26\x7F\xD9\xFF\xD9" >> /tmp/sinewave.wav
		printf "\x7F\x7F\x7F\x7F\x7F\x7F\x7F\x7F" >> /tmp/timer.wav
	done
fi
# *********************************************************
# The line below uses various Linux flavours, "aplay"...
# aplay /tmp/sinewave.wav
# *********************************************************
# Use the OSX default basic command line audio player, "/usr/bin/afplay".
afplay /tmp/sinewave.wav
# aplay /tmp/sinewave.wav
# A demo delay of around 6 seconds, about 1.5 seconds per loop.
for n in {1..4}
do
	# aplay /tmp/timer.wav
	afplay /tmp/timer.wav
done
afplay /tmp/sinewave.wav
# aplay /tmp/sinewave.wav
# *********************************************************
exit 0
# Enjoy finding simple solutions to often very difficult problems...

Results...

Last login: Thu Mar 13 08:19:23 on ttys000
AMIGA:barrywalker~> chmod 755 timer
AMIGA:barrywalker~> ./timer
AMIGA:barrywalker~> ./timer &
[1] 247
AMIGA:barrywalker~> _

You have the added advantage of having a 1 second pure sinewave burst too!

1 Like

Well, it does save a sleep process, I guess. But I think he wants to avoid any new processes.

Hi Perderabo...

Well it __might__ be useful to systems that do not have the "sleep" command.
Android for example.

As it is a wave file the principle could be used to create a function generator
system on just about any platform and use the default player on said platform
as the signal source.

Already working on my BlackBerry 'phone ready for calibrating the AudioScope...

This snippet was just a side effect that could be useful...

Thanks, wisecracker, for those keen insights. It'll take me a bit to unravel & apply, but I know I learned something useful here :wink:

Found the solution to the "read -t" issue in my bumblings about.

This works :):

#!/bin/bash

while :
do
    echo pausing...
    read -t 2
done

Indeed, Ubuntu loves a certain incarnation of dash; which, for some reason, is averse to "read." Nasty habit, that /bin/sh bangline...

(FWIW, I was given to understand bash would automagically "step in" when situations such as this came up (as is done in Parted Magic), hence the default /sh bang usage...)

At any rate, and if folks could humor me a moment longer, how might we go about forming a commandline to point read at empty space (via -u, perhaps?) as opposed to taking user input? Possible???

Thanks again --

Perhaps this will do what you want: read -t3 A </dev/zero

1 Like

Not so much "averse to read" as "averse to nonstandard bash features".

Yes, if using nonstandard bash features, one should really warn people in the hashbang.

1 Like

@Corona688:

Sure thing. What's even more confusing is how certain distros handle the situation with an /sh bangline; some gracefully, some not...

@RudiC:

Awesome! Looks like a good way of working around the need to invoke a new process just to "sleep" in bash :slight_smile:

FWIW, this was the best I could cobble together today:

read -t 3 -s -N 1000000 nop

(UPDATE- Tested here and there: Dairy-free! ;))

If you have a moment, could you give a quick thumbnail sketch to unpack what's going on in your snippet?

Thanks, all!

---------- Post updated at 11:40 AM ---------- Previous update was at 11:33 AM ----------

@RudiC:

Caught a bug!

If one holds the return key, your commandline halts everything with this error:

xrealloc: ../../bash/builtins/../../bash/builtins/read.def:525: cannot allocate 134217840 bytes (536911872 bytes allocated)

Code context:

#!/bin/bash

while :
do
    echo pausing...
    read -t3 A </dev/zero
done

???

This is because /bin/sh gets you "a Bourne shell" but not any particular Bourne shell. What shell you get depends on what shell your system considers a good default. If you don't use extended shell features #!/bin/sh will work on most systems (except perhaps Solaris where /bin/sh can be really, really old.)

So, if you write bash code, you should use #!/bin/bash. If you write ksh code, you should use #!/bin/ksh. #!/bin/sh should only be used for generic Bourne code.