Answering questions about processes via the terminal
As a programmer, when troubleshooting, I often find myself asking: Is that other thing even running? It could be a program incidental to what I’m working on – maybe Node or MySQL. Learning a few Bash commands makes answering questions like “Is this thing running? or “What ports is that program using?” fast.
Note: most of these commands assume a unix-y machine, like Linux or OSX.
What processes are running?
Processes are instances of a programs running on a computer. To see a list of active processes, use the
The above examples uses the
aux options, where:
ashows all processes, even those not owned by the current user
ushows names of users whose file access permissions are used by the process
xused in combination with
a, will show all processes (read the man page for more nuance)
The output should be a long list, in this format:
USER PID %CPU %MEM VSZ RSS TT STAT STARTED TIME COMMAND christianwood 554 4.8 1.3 4527320 107032 ?? R 7:07PM 0:21.94 /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal
ps command becomes more powerful when combined with other commands, like
less command paginates output. Using a pipe to pass the output of the
ps command to
less, we have:
ps aux | less
Once the output is displayed with less, use the
j key to scroll down. To quit, type
Search for processes by name
We can interact with processes by referencing their identifier. Let’s say you want to know what process id Node.js has:
ps aux | grep node
Note: Watch out for grep catching itself in the
psoutput. Some variations of grep avoid this problem.
Say the output says that Node.js has a
PID of 123, you could then stop that process by using the
Read the man page of the kill command for more options.
See live-updating list of processes
To see a live-updating list of processes, use
top. If you’re on a server without a GUI and want to see what programs are using CPU or memory with live updates,
top or its sibling
htop are great tools.
What files and network ports are processes using?
Sometimes you might want to know what files a process is using. We can see them with the
lsof (list of open files) command.
These files might be literal files, like fonts or images in use by Google Chrome.
lsof | grep Chrome | less
This command searches for the list of open files for the word Chrome, and will likely return a long list of files in use by Google Chrome.
Find processes with TCP connections
lsof command can also show all the processes with active TCP connections:
lsof -i TCP
See more options on the
lsof man page.
Find processes using a specific port
This filtering functionality frequently speeds up local development troubleshooting. Say you want to start a new local server with Node.js on port 3000, but receive this error: Error:
listen EADDRINUSE :::3000. This means there is already a process using that port on your machine.
Track it down by running:
lsof -i :3000
The output will include the PID, which you can then use to kill the process.
Overall, knowing how to answer questions about processes improves troubleshooting skills and computer literacy. So go see what’s running on your machine!Written