Linux Commands for Everyday Development: A Comprehensive Guide for Developers
Whether you’re a seasoned developer or just getting started, mastering essential Linux commands can greatly improve your efficiency. This guide covers a curated set of commands frequently used by developers for navigating the file system, managing processes, and viewing logs.
1. Find the Process Using the Port
You can use the lsof
or netstat
command to identify the process ID (PID) occupying the specific port.
Using lsof
:
sudo lsof -i :<port_number>
For example, to find a process using port 8080
:
sudo lsof -i :8080
Sample Output:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 1234 shaon 22u IPv4 0x1234 0t0 TCP *:8080 (LISTEN)
Here:
COMMAND
: The command that started the process (e.g.,node
).PID
: The process ID (e.g.,1234
).USER
: The user who started the process.NAME
: The port and IP address on which it is listening (*:8080
).
Using netstat
:
Alternatively, you can use netstat
to find the PID:
sudo netstat -tulnp | grep :<port_number>
For example, to find a process on port 8080
:
sudo netstat -tulnp | grep :8080
Sample Output:
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1234/node
Here, the process ID is 1234
.
2. Kill the Process Using the PID
Once you have the PID, you can terminate the process using the kill
command.
sudo kill -9 <PID>
For example, if the PID is 1234
:
sudo kill -9 1234
Explanation of Commands:
kill -9
: Sends theSIGKILL
signal to forcefully terminate the process. The-9
flag ensures the process is killed immediately.sudo
: Required for terminating processes owned by other users or root.
3. Combine into a One-Liner Command
You can combine finding and killing into a single command using lsof
and kill
:
sudo kill -9 $(sudo lsof -t -i:<port_number>)
Replace <port_number>
with the desired port, such as:
sudo kill -9 $(sudo lsof -t -i:8080)
What This Command Does:
sudo lsof -t -i:8080
: Finds the PID of the process listening on port 8080.$(...)
: Substitutes the PID into thekill
command.sudo kill -9 <PID>
: Forcefully terminates the process with the specified PID.
4. Verify the Process Has Been Terminated
To confirm the process is no longer using the port, run:
sudo lsof -i :<port_number>
If the command returns no output, it means the process has been successfully terminated.
Quick Reference:
Here’s a summarized version of the steps:
- Find the process:
sudo lsof -i :8080
2. Kill the process:
sudo kill -9 <PID>
Or use the one-liner:
sudo kill -9 $(sudo lsof -t -i:8080)
tail
: View the last few lines of a file, commonly used to monitor log files.
tail: View the last few lines of a file, commonly used to monitor log files.