3

man ps says:

If you want a repetitive update of the selection and the displayed information, use top(1) instead.

... however, in some cases, I don't like the output of top - I would instead like to have the output exactly the same as ps axf:

$ ps axf
  PID TTY      STAT   TIME COMMAND
    2 ?        S      0:00 [kthreadd]
    3 ?        S      0:06  \_ [ksoftirqd/0]
    6 ?        S      0:00  \_ [migration/0]
   11 ?        S<     0:00  \_ [cpuset]
   12 ?        S<     0:00  \_ [khelper]
   13 ?        S<     0:00  \_ [netns]
   15 ?        S      0:00  \_ [sync_supers]
   16 ?        S      0:00  \_ [bdi-default]
   17 ?        S<     0:00  \_ [kintegrityd]
   18 ?        S<     0:00  \_ [kblockd]
   19 ?        S<     0:00  \_ [kacpid]
   20 ?        S<     0:00  \_ [kacpi_notify]
   21 ?        S<     0:00  \_ [kacpi_hotplug]
   22 ?        S<     0:00  \_ [ata_sff]
   23 ?        S      0:00  \_ [khubd]
   24 ?        S<     0:00  \_ [md]
   26 ?        S      0:00  \_ [khungtaskd]
   27 ?        S      0:01  \_ [kswapd0]
   28 ?        SN     0:00  \_ [ksmd]
   29 ?        S      0:00  \_ [fsnotify_mark]
   30 ?        S<     0:00  \_ [aio]
   31 ?        S      0:00  \_ [ecryptfs-kthrea]
   32 ?        S<     0:00  \_ [crypto]
   36 ?        S<     0:00  \_ [kthrotld]
   38 ?        S      0:00  \_ [scsi_eh_0]
   39 ?        S      0:00  \_ [scsi_eh_1]
   40 ?        S<     0:00  \_ [kmpathd]
...

Now, running ps axf repeatedly is not a problem (plenty of suggestions on Repeat a Unix command every x seconds forever - Unix & Linux Stack Exchange); however, as the above snippet shows, its output can be quite larger than the size of a terminal window.

So I was wondering - is there a program which can run a command repeatedly, and collect its output, and display it in something like an ncurses window? I'd ideally like to set ps axf to refresh at half a second - and I'd like to have scrolling (given that the output will overflow terminal window bounds), however, such that if the text display starts vertically from, say, line 6 on top, it stays at that position - even if the latest output of the command has more (or less) lines of text in it than the previous one.

I hoped screen may do something like this, but then, I don't think it implements any scrolling behavior... Is there a program out there that does something like this?

4
  • 1
    Does watch -n 0.5 ps axf do what you want? Commented Feb 9, 2014 at 22:41
  • 1
    You're looking for something like watch but unfortunately there's no scrolling as you describe it there.
    – Marki
    Commented Feb 9, 2014 at 22:41
  • Thanks, @MarkPlotnick and @Marki - unfortunately, there seems to be no scrolling behavior with watch, although, at least it keeps the start of output on top of terminal...
    – sdaau
    Commented Feb 9, 2014 at 22:44
  • 1
    You can scroll in screen, but it's a bit cumbersome (press C-a ESC, then you can scroll). Commented Feb 9, 2014 at 23:56

1 Answer 1

2

This particular point of contention has come up here on U&L a couple of times before. Mainly around the tool watch, which doesn't have this feature.

Generally you have 3 options.

  1. Use watch with this limitation
  2. Use a alternative script (mywatch.sh or watchless) or tool such as pwatch
  3. Use an alternative tool to ps, such as htop or atop

So I would encourage you to let go of ps and use it for what it is. A quick way to get at the state of things when in a shell. If you want to "watch" the state of the various processes running on a system use a tool such as htop. It has a "tree" view similar to the one you're asking about and you can scroll through the output.

NOTE: To toggle "treeview" simply hit the t key while in htop.

   ss of htop

Kernel thread in htop?

If you'd like to see Kernel Thread in htop you can enable them. They're disabled by default. There are 2 ways to do this. You can toggle them on and off using the keyborad shortcut Shift+K.

It's also accessible from the Setup menu, F2. Once you're in the Setup menu you can use the arrow keys (←,↑,→,↓) to move around, and to mark things you use the Spacebar. Once you've picked your changes, hit Esc to get out.

   ss #2

NOTE: In the above screenshot, #1 shows you're in the Setup menu. #2 shows we've used the ↑ and ↓ arrow keys to move to Display Options. To access these options, you'd use the ← and → arrow keys to move over to the "choices" part of the menu, followed by ↑ and ↓ arrows to "select". Once you've selected an option you can use the Spacebar to toggle it.

References

6
  • Many thanks for that answer @slm - it explains well that what is a "state of the art" for this problem (which I wasn't aware of) - so I accepted it for now. I got both atop and htop - and it seems, none of them report kthreadd and its "dependencies"; and that is a part of what I need this for (and which is why I wanted a "generic" application which gives one a curses scrolling window). Thanks again - cheers!
    – sdaau
    Commented Feb 10, 2014 at 12:17
  • PS: pwatch seems to be able to do what I wanted; except it needs handlers for proper termination of ncurses; it also (currently) terminates prematurely if one goes above a certain line in the ps axf output (though scrolling works). But otherwise, that was what I was looking for. Cheers!
    – sdaau
    Commented Feb 10, 2014 at 12:46
  • PSS: Multitail linked from one of the pages on this answer also looks neat, but it needs to be built from source (for me, libncursesw5-dev was also needed); but unfortunately it can scroll "only the last 100 lines", and refresh of the command kills the scroll window (as of v6.0).
    – sdaau
    Commented Feb 10, 2014 at 13:05
  • @sdaau - you can control the scrollback using the -mb x and M lines switches on multitail. I only have 5.2.13 in repos.
    – slm
    Commented Feb 10, 2014 at 13:10
  • @sdaau - Did you try something like this? multitail -R 2 -l "ps axf".
    – slm
    Commented Feb 10, 2014 at 13:15

You must log in to answer this question.

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