tail does not block
As always: for everything there is an answer that is short, easy to understand, easy to understand and completely incorrect. Here tail -f/dev/null falls into this category;)
If you look at this with strace tail -f/dev/null you will notice that this solution is far from blocking! This is probably even worse than the sleep solution in question, since it uses (under Linux) precious resources such as the inotify system. Also other processes that write to /dev/null make the tail loop. (On my Ubuntu64 16.10, this adds a few 10 system calls per second on an already loaded system.)
The question was for the lock command
Unfortunately, there is no such thing ..
Read: I don't know how to archive this using the shell directly.
Everything (even sleep infinity ) can be interrupted by some signal. Therefore, if you want to be really sure that it is not exclusively returning, it should work in a loop, as you already did for sleep . Note that (on Linux) /bin/sleep apparently limited to 24 days (look at strace sleep infinity ), so probably the best thing you can do is:
while :; do sleep 2073600; done
(Note that I believe that the sleep cycle is internally for higher values than 24 days, but this means: it does not block, it loops very slowly. So why not move this cycle outward?)
.. but you can get pretty close with an unnamed fifo
You can create something that really blocks, as long as there are no signals sent to the process. The following uses bash 4 , 2 PID 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 was it built
read blocks if there is no input (see some other answers). However, tty (aka stdin ) is usually not a good source, as it closes when the user logs out. It can also steal some input from tty . Not good.
To make the read block, we need to wait for something like fifo that will never return anything. In bash 4 there is a team that can provide us with such a fifo : coproc . If we also wait until a blocking read (which is our coproc ), we are done. Unfortunately, for this you need to keep open two PID and fifo .
Option named fifo
If you haven’t bothered to use a named fifo , you can do it like this:
mkfifo "$HOME/.pause.fifo" 2>/dev/null; read <"$HOME/.pause.fifo"
Not using a read loop is a bit messy, but you can use this fifo as many times as you like and end read using touch "$HOME/.pause.fifo" (if touch "$HOME/.pause.fifo" more than one reading, everything stops immediately).
Or use the Linux system call pause()
For endless locking, there is a call to the Linux kernel called pause() , which does what we want: wait forever (until a signal arrives). However, there is no user program for this yet.
FROM
Creating such a program is easy. Here is a piece of code to create a very small program for Linux called pause which pauses indefinitely (you need 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 up one PID. The solution can also be improved by some I / O redirection, freeing up unused FDs. It's up to you.)
How it works (I think): ctypes.CDLL(None) loads the standard C library and runs the pause() function in it in some additional loop. Less efficient than version C, but works.
My recommendation for you:
Stay in a looped dream. It is easy to understand, very portable and blocks most of the time.