Bash: infinite sleep (infinite blocking)

sleep infinity docker
bash sleep
ps sleep infinity
sleep infinity alpine
bash wait
bash infinite loop with counter
bash sleep for 5 mins
sleep infinity & wait

I use startx to start X which will evaluate my .xinitrc. In my .xinitrc I start my window manager using /usr/bin/mywm. Now, if I kill my WM (in order to f.e. test some other WM), X will terminate too because the .xinitrc script reached EOF. So I added this at the end of my .xinitrc:

while true; do sleep 10000; done

This way X won't terminate if I kill my WM. Now my question: how can I do an infinite sleep instead of looping sleep? Is there a command which will kinda like freeze the script?

Best regards

sleep infinity does exactly what it suggests and works without cat abuse.

How to do nothing forever in an elegant way?, sleep infinity is the clearest solution I know of. You can use infinity because sleep accepts a floating point number*, which may be decimal, hexadecimal, infinity,� H ow do I write an infinite loop in Bash script under Linux or UNIX like operating systems? An infinite loop is nothing but a sequence of instructions which loops endlessly, either due to the loop having no terminating condition, having one that can never be met, or one that causes the loop to start over.

tail does not block

As always: For everything there is an answer which is short, easy to understand, easy to follow and completely wrong. Here tail -f /dev/null falls into this category ;)

If you look at it with strace tail -f /dev/null you will notice, that this solution is far from blocking! It's probably even worse than the sleep solution in the question, as it uses (under Linux) precious resources like the inotify system. Also other processes which write to /dev/null make tail loop. (On my Ubuntu64 16.10 this adds several 10 syscalls per second on an already busy system.)

The question was for a blocking command
Unfortunately, there is no such thing ..

Read: I do not know any way to archive this with the shell directly.

Everything (even sleep infinity) can be interrupted by some signal. So if you want to be really sure it does not exceptionally return, it must run in a loop, like you already did for your sleep. Please note, that (on Linux) /bin/sleep apparently is capped at 24 days (have a look at strace sleep infinity), hence the best you can do probably is:

while :; do sleep 2073600; done

(Note that I believe sleep loops internally for higher values than 24 days, but this means: It is not blocking, it is very slowly looping. So why not move this loop to the outside?)

.. but you can come quite near with an unnamed fifo

You can create something which really blocks as long as there are no signals send to the process. Following uses bash 4, 2 PIDs and 1 fifo:

bash -c 'coproc { exec >&-; read; }; eval exec "${COPROC[0]}<&-"; wait'

You can check that this really blocks with strace if you like:

strace -ff bash -c '..see above..'
How this was constructed

read blocks if there is no input data (see some other answers). However, the tty (aka. stdin) usually is not a good source, as it is closed when the user logs out. Also it might steal some input from the tty. Not nice.

To make read block, we need to wait for something like a fifo which will never return anything. In bash 4 there is a command which can exactly provide us with such a fifo: coproc. If we also wait the blocking read (which is our coproc), we are done. Sadly this needs to keep open two PIDs and a fifo.

Variant with a named fifo

If you do not bother using a named fifo, you can do this as follows:

mkfifo "$HOME/.pause.fifo" 2>/dev/null; read <"$HOME/.pause.fifo"

Not using a loop on the read is a bit sloppy, but you can reuse this fifo as often as you like and make the reads terminat using touch "$HOME/.pause.fifo" (if there are more than a single read waiting, all are terminated at once).

Or use the Linux pause() syscall

For the infinite blocking there is a Linux kernel call, called pause(), which does what we want: Wait forever (until a signal arrives). However there is no userspace program for this (yet).

C

Create such a program is easy. Here is a snippet to create a very small Linux program called pause which pauses indefinitely (needs diet, gcc etc.):

printf '#include <unistd.h>\nint main(){for(;;)pause();}' > pause.c;
diet -Os cc pause.c -o pause;
strip -s pause;
ls -al pause
python

If you do not want to compile something yourself, but you have python installed, you can use this under Linux:

python -c 'while 1: import ctypes; ctypes.CDLL(None).pause()'

(Note: Use exec python -c ... to replace the current shell, this frees one PID. The solution can be improved with some IO redirection as well, freeing unused FDs. This is up to you.)

How this works (I think): ctypes.CDLL(None) loads the standard C library and runs the pause() function in it within some additional loop. Less efficient than the C version, but works.

My recommendation for you:

Stay at the looping sleep. It's easy to understand, very portable, and blocks most of the time.

Bash: infinite sleep (infinite blocking), For the infinite blocking there is a Linux kernel call, called pause() , which does what we want: Wait forever (until a signal arrives). However there is no� Bash: infinite sleep (infinite blocking) Ask Question Asked 10 years, 2 months ago. Active 7 months ago. Viewed 87k times 158. 23. I use startx

Maybe this seems ugly, but why not just run cat and let it wait for input forever?

Bash: infinite sleep (infinite blocking), Bash: infinite sleep (infinite blocking). 别忘想泡老子 关注. 发布时间:2019-01-21 08:14. I use startx to start X which will evaluate my .xinitrc . In my .xinitrc I start my � The Linux sleep command, among other things, pauses a bash script. On its own, the sleep command isn't very useful. However, as part of a script, it can be used in many ways. For example, you can use it to pause the script before retrying a command that failed the first time.

TL;DR: sleep infinity actually sleeps the maximum time allowed, which is finite.

Wondering why this is not documented anywhere, I bothered to read the sources from GNU coreutils and I found it executes roughly what follows:

  1. Use strtod from C stdlib on the first argument to convert 'infinity' to the double precision. So, assuming IEEE 754 double precision the 64-bit positive infinity value is stored in the seconds variable.
  2. Invoke xnanosleep(seconds) (found in gnulib), this in turn invokes dtotimespec(seconds) (also in gnulib) to convert from double to struct timespec.
  3. struct timespec is just a pair of numbers: integer part (in seconds) and fractional part (in nanoseconds). Naïvely converting positive infinity to integer would result in undefined behaviour (see §6.3.1.4 from C standard), so instead it truncates to TYPE_MAXIMUM (time_t).
  4. The actual value of TYPE_MAXIMUM (time_t) is not set in the standard (even sizeof(time_t) isn't); so, for the sake of example let's pick x86-64 from a recent Linux kernel.

This is TIME_T_MAX in the Linux kernel, which is defined (time.h) as:

(time_t)((1UL << ((sizeof(time_t) << 3) - 1)) - 1)

Note that time_t is __kernel_time_t and time_t is long; the LP64 data model is used, so sizeof(long) is 8 (64 bits).

Which results in: TIME_T_MAX = 9223372036854775807.

That is: sleep infinite results in an actual sleep time of 9223372036854775807 seconds (10^11 years). And for 32-bit linux systems (sizeof(long) is 4 (32 bits)): 2147483647 seconds (68 years; see also year 2038 problem).


Edit: apparently the nanoseconds function called is not directly the syscall, but an OS-dependent wrapper (also defined in gnulib).

There's an extra step as a result: for some systems where HAVE_BUG_BIG_NANOSLEEP is true the sleep is truncated to 24 days and then called in a loop. This is the case for some (or all?) Linux distros. Note that this wrapper may be not used if a configure-time test succeeds (source).

In particular, that would be 24 * 24 * 60 * 60 = 2073600 seconds (plus 999999999 nanoseconds); but this is called in a loop in order to respect the specified total sleep time. Therefore the previous conclusions remain valid.


In conclusion, the resulting sleep time is not infinite but high enough for all practical purposes, even if the resulting actual time lapse is not portable; that depends on the OS and architecture.

To answer the original question, this is obviously good enough but if for some reason (a very resource-constrained system) you really want to avoid an useless extra countdown timer, I guess the most correct alternative is to use the cat method described in other answers.

Bash Infinite Loop Examples, How do I write an infinite loop in Bash script under Linux or UNIX like operating systems? while true; do echo 'Hit CTRL+C'; sleep 1; done� while true; do foo; sleep 2; done By the way, if you type it as a multiline (as you are showing) at the command prompt and then call the history with arrow up, you will get it on a single line, correctly punctuated.

sleep infinity looks most elegant, but sometimes it doesn't work for some reason. In that case, you can try other blocking commands such as cat, read, tail -f /dev/null, grep a etc.

Bash While Loop Examples, How do I use bash while loop to repeat specific task under Linux / UNIX operating system? How do I set infinite loops using while statement? Subject: Re: splice: infinite busy loop lockup bug: From: Tetsuo Handa <> Date: Fri, 7 Aug 2020 23:17:32 +0900

Linux Sleep Command (Pause a Bash Script), The sleep command is useful when used within a bash shell script, for example, when In the first line, we are creating an infinite while loop . wait is a built-in command of Linux that waits for completing any running process.wait. command is used with a particular process id or job id.. When multiple processes are running in the shell then only the process id of the last command will be known by the current shell.

sleep forever bash, Bash: infinite sleep (infinite blocking) Ask Question (behaves like exec sleep infinity if it is the last statement. docker run -d debian sleep 300 Which has the� sleep is a very popular command and we can start sleep from 1 second: # wait one second please sleep 1 but what the alternative if I need to wait only 0.1 second or between 0.1 to 1 second ? remark: on linux or OS X sleep 0.XXX works fine , but on solaris sleep 0.1 or sleep 0.01 - illegal syntax

sleep infinity - UNIX and Linux Forums, sleep infinity. Original Post by indyxandy. indyxandy. This might be one of the dumbest questions you've got, but please bear with me: I am a UNIX beginner.

Comments
  • Cool. Unfortunately my busybox does not understand.
  • BSD (or at least OS X) doesn't understand sleep infinity either, though it was a cool thing to learn about for Linux. However, while true; do sleep 86400; done ought to be an adequate substitute.
  • Regarding this, I made some research I documented in a separate answer. To summarize: infinity is converted in C from "string" to a double. Then that double is truncated to the maximum values allowed timespec, which means a very large amount of seconds (architecture-dependant) but, in theory, finite.
  • @Andrew Normally you do not need the trap (which modifies the behavior of the shell to signals) nor the background (which allows the shell to intercept signals from the terminal, like Strg+C). So sleep infinity is enough (behaves like exec sleep infinity if it is the last statement. to see the difference use strace -ffDI4 bash -c 'YOURCODEHERE'). The looping sleep is better, because sleep can return in certain circumstances. For example you do not want X11 to shut down suddenly on a killall sleep, just because .xstartup ends in sleep infinity instead of a sleep loop.
  • May be a little obscure, but s6-pause is a userland command to run pause(), optionally ignoring various signals.
  • @Tino /bin/sleep is not capped at 24 days as you say. It would be nice if you could update that. On Linux right now, this code is active. It caps individual nanosleep() syscalls to 24 days, but calls them in a loop. So sleep infinity should not exit after 24 days. The double positive infinity gets converted to a struct timespec. Looking at rpl_nanosleep in GDB, infinity gets converted to { tv_sec = 9223372036854775807, tv_nsec = 999999999 } on Ubuntu 16.04.
  • @nh2 It was already mentioned in the text that sleep probably loops instead of being fully blocking. I edited it now slightly to hopefully make this fact a bit more clear. Please note this "probably", because from strace alone I cannot prove the fact that there is really some looping code compiled in sleep, and I do not want to wait 24 days just to test this (or decompile /bin/sleep). It is always better to program defensively, if there is no hard mathematical proof, that something really is, as it seems to be. Also never trust anything: killall -9 sleep
  • The pause() option can be done pretty easily with perl: perl -MPOSIX -e 'pause()'
  • This doesn't work if you don't have a hanging pipe from which to read. Please advise.
  • @Matt, maybe make a pipe and cat it? mkfifo pipe && cat pipe
  • What @twalberg says, but additionally you can immediately reassign to 3 and unlink it, as shown here: superuser.com/a/633185/762481
  • In the next coreutils, sleep infinity will now actually sleep forever without looping: lists.gnu.org/archive/html/bug-gnulib/2020-02/msg00081.html
  • tail -f /dev/null was also a working solution for me on a SaaS platform
  • tail -f /dev/null also has the advantage of not consuming stdin. I have used it for that reason.