What do you do when a Linux data center server is running slowly? One of the first things you might do is check a network connection. After that, you’re probably going to need to find out what services/software have gone rogue and are gobbling up precious system resources. There are a number of ways to do that. For example, you could issue the top command, which will list out, in real-time, running processes. From there you can find a process ID and see how much memory usage said process is using.
Or you can go another route and use a couple of commands to find that Process ID (PID) by name. There are two commands in particular, both of which every Linux administrator should know. I want to demonstrate the usage of those commands, so hopefully your Linux admin job might be a bit easier. I’ll be demonstrating on Ubuntu Server 18.04, but the commands are available (and are used in the same fashion) on all Linux platforms.
The pidof command is the first we’ll look at. As you might expect, it will tell us the PID of a service. This command is found on your Linux servers out of the box, so there’s no need to install anything.
Let’s say you have a LAMP server in your data center and Apache is behaving badly (or at least you assume it is). To troubleshoot, you might need to find the PID of Apache. Fortunately, you have the pidof command, so you could issue:
In the case of Apache, you might be returned a number of PIDs (Figure A).
You now have every Process ID of your currently running Apache server. Of course, with a process like Apache, on the off chance it is behaving, you might want to do a quick restart using a command like sudo systemctl restart apache2. But if the service doesn’t respond to a restart, what do you do? Fortunately, you have those PIDs, so you can always kill the service (using the kill PID command, where PID is the PID of the service to be killed). Killing a service like Apache should be done only as a last resort (always start with a tool like systemctl). But having those PIDs in hand can make your job considerably easier.
SEE: Linux distribution comparison chart (Tech Pro Research)
The pgrep command functions in similar fashion to pidof, the main difference being that it can look up a process based on a name or other attributes. For example, pgrep can look up PIDs associated with a group. On Ubuntu, most Apache content tends to be attached to the www-data group. With pgrep, it’s easy to find out what PIDs are associated with that group. Issue the command:
pgrep -G www-data
The output of the above command will list all the PIDs that belong to the group in question (Figure B).
The output of that command might not do us any good. After all, how do we know what those PIDs belong to (outside of a group)? We might need to know not only the PIDs for the group, but the services attached to the PIDs. To get this information, we add the -l option like this:
pgrep -l -G www-data
Now we see the PIDs and the services listed (Figure C).
SEE: How to find files in Linux with grep: 10 examples (free TechRepublic PDF)
You could also use pgrep to list a process by user. If you issued the command pgrep -U root, you’d see every PID that belonged to the root user (which would be a significant number). If you issued that same command using a specific user (other than root), chances are the list would be considerably shorter. For instance, pgrep -U jack (on my server) returns the PIDs:
1225 1234 1251 1352
To find out what services are associated with those PIDs, issue the command:
pgrep -l -U jack
Now we see what services and PIDs belong to the user jack (Figure D).
There’s more to pidof and pgrep than I’ve outlined here. The best way to learn more is by issuing the commands man pidof and man pgrep to read the manual pages for each command. These two commands should easily fit in your Linux admin toolbox and will help keep your data center humming.
Image: Jack Wallen