ps -xa | grep node to kill specific process

I am currently using the following command to get a process:

ps -xa | grep node

Which results in the following:

13611 ?        Sl     0:03 /opt/brackets/Brackets-node /opt/brackets/node-core
20713 pts/1    Sl     0:00 node --harmony app.js
20838 pts/1    S+     0:00 grep node

I use the command kill -9 20713 to kill the node --harmony app.js process.

How can I kill the node --harmony app.js every single time with one command? I am tired of typing in the process number every time.


Use pkill:

pkill node

This would match the other command as well, so fine tune it:

pkill -f "node --harmony app.js"

This matches the full command line (-f) exactly, so it should only hit the desired command.


You can use killall. The simplest syntax is:

killall "Process_name"

In you case:

killall "node --harmony app.js"

The upside of killall is that it will match the exact name so there is no chance of killing other processes unwantedly.

Although you can use -r option to express the process as a regular expression pattern like pkill.

Check man killall for more info.


Use an alias and your own "command word" for it.

E.G.go to your home Dir and create the file .bash_aliases

Put the following text into the file

alias nerdalert='pkill -f "node --harmony app.js"'

and then search in your home folder for your .bashrc looking for this part and make sure it's not commented out.

# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Now type into your terminal

source /home/${USER}/.bash_aliases

and try your brand new "command" a.k.a. alias

nerdalert

Enjoy ^^