20

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.

Peter Mortensen
  • 1,029
  • 1
  • 8
  • 10
srigi
  • 303
  • 2
  • 6

6 Answers6

26

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(); }
Toby Speight
  • 8,460
  • 3
  • 26
  • 50
ilkkachu
  • 133,243
  • 15
  • 236
  • 397
18

Just add a sleep command.

while true; do sleep 600; done

will sleep for 10 minutes between loops.

dr_
  • 28,763
  • 21
  • 89
  • 133
18

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
Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501
deltab
  • 443
  • 2
  • 9
16

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
rudimeier
  • 9,967
  • 2
  • 33
  • 45
  • 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 < – Olivier Dulac May 19 '17 at 14:29
  • @OlivierDulac thanks, this was a typo, not a wanted bashism ;) – rudimeier May 19 '17 at 14:31
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)

Olivier Dulac
  • 5,924
  • 1
  • 23
  • 35
  • 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

ivanivan
  • 4,870
  • 1
  • 9
  • 20