21

I am trying to run top with multiple PIDs using -p option and xargs. However, top fails to run with error top: failed tty get:

$ pgrep gvfs | paste -s -d ',' | xargs -t top -p
top -p 1598,1605,1623,1629,1635,1639,1645,1932,2744
top: failed tty get

I used the -t option for xargs to see the full command which is about to be executed. It seems fine and I can run it successfully by hand:

top -p 1598,1605,1623,1629,1635,1639,1645,1932,2744

However, it does not run with xargs. Why is that?

2 Answers 2

33

Turns out that there is a special option --open-tty in xargs for interactive applications like top. From man xargs:

   -o, --open-tty
          Reopen stdin as /dev/tty in the child process before
          executing the command.  This is useful if you want xargs
          to run an interactive application.

The command to run top should be:

pgrep gvfs | paste -s -d ',' | xargs --open-tty top -p
1
  • 1
    That's a useful feature to know about. When running curses-based applications, I always used command substitution (as in the other answer). While this option is not specified by POSIX, I see it is supported by GNU and BSD implementations of xargs. Jan 15, 2022 at 19:29
21

top is an interactive program, for instance you can type i to toggle showing idle processes. Whilst it could arrange to read from /dev/tty in practice it expects that stdin is connected to the terminal.

For your example just use command substitution rather than xargs, e.g.

top -p "$(pgrep gvfs | paste -s -d ',')"

The shell first runs the pgrep and paste, gets the output from those commands and then invokes top with that output.

2
  • 1
    This is command substitution, not process substitution. Process substitution is with <( ... )
    – user000001
    Jan 17, 2022 at 20:29
  • 1
    @user000001 True, edited -thanks.
    – icarus
    Jan 17, 2022 at 22:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .