Home > linux > How To: Run top command on a remote host

How To: Run top command on a remote host

Very often I run into issues where a remote server has high resource usage and at the same time I attempt to log in to run the top command to get an idea what process is the culprit.

If you’ve been in this situation, you know that logging onto a server while it’s down to it’s knees is almost impossible.  With the amount of time it takes to login and then run top interactively, chances are you’ll never get to see what’s going on real-time.

That’s why it’s highly recommended that you monitor your servers on regular basis and have some sort of alerting mechanism notifying you of load issues whenever they’re happening.  In this article, we’re going to discuss a very simple way of doing that without logging into server manually.

To login to server1 using SSH and display all processes non-interactively:

$ ssh user@server1 'top -b -n 1'

To display only the top portion of the table detailing number of tasks, load average, cpu, memory and swap usage:

$ ssh user@server1 'top -b -n 1 | head -5'

To display processes that belong to user apache:

$ ssh user@server1 'top -u apache -b -n 1'

To monitor your remote hosts, you can put a quick script together and run via your cron every x minutes and compile daily report of overall usage.  Of course it’s given that you should setup password-less ssh session so you don’t get prompted to enter a password.

Categories: linux Tags:
  1. No comments yet.
  1. No trackbacks yet.