I want to stop Node server on Linux, but when I try to stop it, it automatically starts again with a new PID. How can I stop this completely? Here you can see I tried to stop Nginx and Node process.
I have tried tried these commands:
kill pid
Kill -9 pid
killall node <<command not working
killall -s KILL node <<< not workingWhen I tried to kill process 3814 it shows error No such process, how can I stop node server???
41 Answer
You are misunderstanding the output of ps. Your first command, sudo systemctl stop nginx is the right way to stop a running service and did actually work. The line you see in the ps isn't a running nginx process, that is the grep process you launched:
$ ps aux | grep foo
terdon 642773 0.0 0.0 8944 2384 pts/23 S+ 13:40 0:00 grep --color fooWhen you run ps | grep, that is a process so that is also included in the output of ps. And, since your grep contains the string nginx, the grep itself is included in the output of grep. The usual ways around this are:
Use
pgrepinstead ofgreppgrep nginxThat will only return anything (a list of PIDs) if there is a running
nginxprocess.Use a character class in
grepso that you can still match the process, but will ignore grep itself:ps aux | grep '[n]ginx'
Because the grep process now contains [n]ginx instead of nginx, it will not be matched by itself.
Since these are set up as services and, presumably, set to launch automatically, killing them just makes them restart. To stop them, you should always use systemctl stop nginx or, if your system supports it service nginx stop.