Display PID and Port of running processes - linux

I have a task where I have to display the PID and Port number of running processes/tasks in Linux. What I have done so far is:
ps -ef | awk '/[t]ick/{print "PID="$2" "$9}'
Can anyone tell me how do I also display the port of the returned results? Here is what Im getting returned currently:

Related

Port is already in use - for any port on Mac

So recently I got a Macbook and I have cloned my project on it which is a MERN stack application. The port in my .env is 5000. When I attempt to start up the server I get the error that the port is already in use.
I figured "hmm ok, i'll change it to 5100". That worked, but only for that time, the next time I tried to run it i got the same error for port 5100.
Anyone have any idea what's going on?
Check the process Id
lsof -nP -iTCP -sTCP:LISTEN | grep 5000
Check the process name
ps -Ao user,pid,command | grep -v grep | grep <<pid>>
which will output something similar to this, which means your mac OS is utilising port 5000
622 /System/Library/CoreServices/ControlCenter.app/Contents/MacOS/ControlCenter
This is because new AirPlay functionality in mac Monterey version is utilising port 5000. To disable that you can
go to system preferences -> search for sharing -> untick AirPlay Receiver in left side panel
You could use
netstat -anvp tcp | awk 'NR<3 || /LISTEN/'
(credits: https://apple.stackexchange.com/a/117648)
and use
ps aux PID
to see the correspoding proccess. (PID is number from PID columen, ofc.)
This is due to the new AirPlay functionality on Monterrey Os.
Control Center stops listening to those ports when you turn off “AirPlay Receiver”.
In the “Sharing” System Preference, so uncheck airplay receiver:
After that you must kill the process that take the 500 port
lsof -nP -iTCP -sTCP:LISTEN | grep 5000
That show you which process to kill
Then, you can use the port 5000 again.

Find the port number of a specific process by using the PID

I am a newbee in this. I am using Netstat to find the port number of a specific PID. I have used the following comman to find the PIDs of all the process. My goal is to search for the port number only by using the PID of a process.I am using Linux, certain options like find,findstr is not working, so I tried the Following commands. Kindly give some suggestion. Thanks in advance
netstat -anop
To show the port number I have used the following code by PID
sudo netstat -anpe | grep ${#PID}
The port number is shown after the local or foreign address, is the number followed by the semicolumn, i.e. :
192.168.43.6:42010 198.252.206.25:443

Get stdout and stderr of a nodejs application running in background as process

there is an existing nodejs code on a ubuntu server running in the background. This existing code is having some issues and I want to see the console.logs of this code running.
I have found the process id of by:
ps -aef | grep node
which gives
aman 20897 1 0 Sep26 ? 03:07:06 node Monitor/Broker.js
Can someone help with how do I find the logs/stdout/stderr for this process ?
Thanks
edit
output of lsof -p 20897

Find out all running node.js application with pid and port

I have to find all running node.js app on machine with pid and port along with path and then following path I would like to find all classes then functions inside classes. Example suppose we have 2 separate independent node application with different folder and want to find as mentioned above.I tried hard but not find solution. I have to implement this functionality for client project. Is this possible and if possible then how it be done?
Any insight would be appreciated.
Thanks! Any help is appreciated
Yes, there are a couple of ways to do this -
pgrep looks through the currently running processes and lists the process IDs which matches the selection criteria to stdout.
pgrep node
Process command (ps)
ps ax will give the full list of processes and adding a 'u' option gives detailed information. i.e. ps aux
To search for a particular process grep command is used, so for nodejs, it would be ps aux | grep node
Network Statistics (netstat)
netstat -a | more : To show both listening and non-listening sockets
netstat -at : To list all tcp ports.
netstat -l : To list only the listening ports.
netstat -lt : To list only the listening tcp ports.
netstat -pt : To display the PID and program names
So as chris-lam has suggested netstat -lntp | grep node would list all the listening TCP ports running as a node process.
To use it within code following piece of code can be helpful -
const {exec} = require('child_process');
exec('ps aux | grep node', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
You can find the port and PID with the netstat command.
netstat -lntp | grep node
ps allows you to find the full command of the process:
ps aux | grep node
After some googling and researching, this is the way I found the easier one for me.
I didn't found yet a one-liner, so my solution is composed by 2 commands:
ps aux | grep node
it wil return all node process with pids and paths
netstat -lntp | grep node
it will return pids and ports
Using them together with
ps aux | grep node && netstat -lntp | grep node
Will give you something like this:
So if you want to know which app is using which port, you can look for the port from the output of second command, take the pid from the last column and from that you can lookup the pid in the output from first command: from there you should be able to understand the path of the program using that port.
It's not very elegant but it's the easier way I found

How to find out which port number a process is using and the process using a specific port number

I'm beginner with openhab, after launching the server /etc/init.d/openhab2 start i got that [ ok ] Starting openhab2 (via systemctl): openhab2.service, but I still have no access to the platform via http://localhost:8080/
I want to know :
which process is using port 8080
which port openhab runs on
thanks
you can use lsof,
lsof | grep TCP | grep 8080 and lsof | grep openhab | grep TCP
if a process forks and the child process is the one that is using a port it may not work as expected

Resources