14

I'm looking for a Linux command that does literally nothing, doesn't output anything, but stays alive until ^C.

while true; do; done is not a good solution, because it is CPU intensive.

17

Just add a sleep command.

while true; do sleep 600; done

will sleep for 10 minutes between loops.

  • 4
    You're thinking too small, just go with sleep 999999999. It saves the hassle of a shell script and forking another process every ten minutes. :) – ilkkachu May 19 '17 at 14:14
  • 3
    GNU sleep will take "infinity" as an argument, too – Jeff Schaller May 19 '17 at 15:50
  • 1
    @Jeff, so will the sleep builtin of ksh93, but not that of mksh. – Stéphane Chazelas May 19 '17 at 16:25
23

If we look at system calls, there's actually one that does exactly that, pause(2):

pause() causes the calling process (or thread) to sleep until a signal is delivered ...

Of course, then we'd just need a program that uses it. Short of compiling the two-liner C program below, the easiest way is probably with Perl:

perl -MPOSIX -e pause

In C:

#include <unistd.h>
int main(void) { return pause(); }
14

Since you've mentioned ctrl-C I assume that you want to use it in interactive terminal. So you may just wait for input.

$ read

or just use arbitrary other commands which read from stdin like cat. They do "nothing" as long as there is no input.

$ cat >/dev/null

or even better without using stdin:

$ tail -f <<EOF
EOF
  • 1
    This may however be tricky in a shell script if standard input is being looped over and read or cat incorrectly consume from that... – thrig May 19 '17 at 14:11
  • 1
    as a variant that doesn't use stdin: tail -f /an/existing/regular/file >/dev/null : this will sit waiting for new addition to /an/existing/regular/file (doesn't work on some files: tail -f /dev/null will exit immediately. But will work for all regular files. If that file is not growing, the command will eat little cpu) – Olivier Dulac May 19 '17 at 14:19
  • @rudimeier : well the "<(true)" part is not really portable in itself, if one happen to use "old bashes" or other old shells with no support for "<(...)" – Olivier Dulac May 19 '17 at 14:24
  • @OlivierDulac found the trick, see my edited answer. – rudimeier May 19 '17 at 14:27
  • @rudimeier: nice, but just do a regular tail -f <<EOF to be a tiny bit more portable ^^ (you already have +1 from me anyway) – Olivier Dulac May 19 '17 at 14:29
12

GNU sleep and the sleep builtin of ksh93 (but not mksh) accept any floating point number, not just integers, so you can do this:

sleep infinity
6

you can:

tail -f /an/existing/regular/file >/dev/null

This will not use stdin (as read would) and will sit waiting for new addition to /an/existing/regular/file (doesn't work on some files: tail -f /dev/null will exit immediately. But will work for all regular files. If that file is not growing, the command will eat little cpu)

  • Except on systems that use APIs like Linux' inotify, that will typically do one read() system call per second though. – Stéphane Chazelas May 19 '17 at 16:28
  • @StéphaneChazelas yes, it is not "doing nothing", but hopefully not much. I'm sure there are better options out there (sleep being quite good already), I just showed an alternative. – Olivier Dulac May 19 '17 at 16:30
5

Not for forever, but there is sleep. You could combine your while loop with sleep - doesn't even seem to tickle the cpus in my gkrellm monitor.
dr01 types faster than I do :) ... so more info - your cpu spiking is because it has to continually process the logic check with no pause between....

while true
do
  sleep 100
done

Or as a one-liner

while true; do sleep 100; done

  • Thank you both. Adding sleep indeed solves CPU load problem. – srigi May 19 '17 at 14:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.