2

I am trying to monitor a certain process with top command and have launched top with

top -p <pid1> 

The process quits after some operations and I relaunch the process. This time the process starts with another pid, say pid2.

Now is there a way to add the pid2 of the new process to the same top running instance? Additionally pid1 may be removed from the top's pid filter.

I have formatted the top display to suit my need and stopping the top and relaunching requires to reconfigure the top output each time.

2
  • Configure the output with command line parameters also or use ~/.toprc. Commented Sep 18, 2018 at 6:29
  • @IporSircer Not all implementations of top uses a configuration file.
    – Kusalananda
    Commented Sep 18, 2018 at 6:53

1 Answer 1

2

I usually use top with pgrep. pgrep is used to find a PIDs by process name.

pgrep sshd
1364
2144
2155

Now we got all the PID, it is time to show on top:

top -p `pgrep -d',' sshd`

You can monitor a process by a name with the help of watch tool. type:

watch -n 5 "top -cbn 1 -p `pgrep -d',' sshd`"

to quit press ctrl-c. It surely will refresh the top process by 5 seconds. By using watch command you cannot control the top task window.

or you can apply a filter while in top task window and type letter o, and type COMMAND=sshd and press enter.

And howto save top config? Just press letter W. But FYI letter W will not save a filter. So you have to apply the filter manually every time you start top.

2
  • Thanks a lot for your answer. But I was looking for a way the top automatically reloads if one of the sshd process in your example restarts. I am having a separate console for monitoring where the top is running and wanted to keep it undisturbed.
    – Bala TJ
    Commented Sep 18, 2018 at 8:37
  • You can type letter o while in task window. and type COMMAND=sshd and press Enter. I think it should do.
    – affan
    Commented Sep 18, 2018 at 8:54

You must log in to answer this question.

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