List Node process and kill specific process if needed

This post provides instructions on how to list running nodeJS application, and  how to kill specific process.

  • List node process:
$ ps -e|grep node
  • If you want know, how many nodejs processes running, you can use the following command
$ ps -aef | grep node

It will give list of nodejs process with it’s project name. This is helpful when you are running multipe nodejs application & you want kill specific process for the specific project.

  • Kill  specific process using the following command:
$kill -9 XXXX

XXXX is the process number to be killed.

  • You can kill all node processes using 
$ pkill node

If all those kill process commands mentioned above don’t work for you, probably you will need to check if you were using any other packages to run your node process.

For example, if you were running your node process using PM2(a NPM package), the kill [processID] command indeed will disable the process but it will keep the port that you ran the nodejs app occupied. You will  need to go into PM2 and dump all node process to free up the port again.

 

You can use the following comment to check all running process, including node and java apps etc.

$ ps -ef

you will see a list with the headers as the following line:

UID   PID   PPID   C   STIME   TTY   TIME   CMD

 Among the line,

STIME means the time your app starts,

TIME   means total CPU usage of the corresponding process

and

CMD is the app’s name running (i.e., name of the process, including arguments, if any)

 

if you only want to list node process,

use the following command,

$ ps -ef | grep node

if you only want to list java process,

use the following command,

$ ps -ef | grep java

You got the idea…

-e and -f are options to the ps command, and pipes  (i.e., | ) take the output of one command and pass it as the input to another. Here is a full breakdown of this command:

ps – list processes

-e – show all processes, not just those belonging to the user

-f – show processes in full format (more detailed than default)

command 1 | command 2 – pass output of command 1 as input to command 2

grep find lines containing a pattern

processname – the pattern for grep to search for in the output of ps -ef

So, the following command means:

look for lines containing processname in a detailed overview/snapshot of all current processes, and display those lines

ps -ef | grep processname

 

Leave a Reply

Your email address will not be published. Required fields are marked *