36

I started a React app with npm start with start defined in package.json:

{
  "name": "testreactapp",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-scripts": "1.0.10"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

I want to stop it now, without closing the terminal. How do I do that?

Tried: npm stop testrectapp but that throws error that it needs a script

Then tried: npm run stop with script "stop": "pkill --signal SIGINT testreactapp" that throws error 'pkill is not recognized as a command'

Edit

Running ps in bash shows:

      PID    PPID    PGID     WINPID   TTY         UID    STIME COMMAND
     6652       1    6652       6652  ?         197612 19:52:49 /usr/bin/mintty
     1092       1    1092       1092  ?         197612   Jul 25 /usr/bin/mintty
    11092    6652   11092      10060  pty1      197612 19:52:49 /usr/bin/bash
    13868    1092   13868        992  pty0      197612   Jul 25 /usr/bin/bash
    11428   13868   11428      17340  pty0      197612 12:48:27 /usr/bin/ps
    11672    1092    1092      11672  ?         197612   Jul 25 /usr/bin/mintty <defunct>

Don't see the app there?

user1837293
  • 1,466
  • 3
  • 16
  • 30

12 Answers12

25

Hit your keyboard shortcut for stopping terminal commands (usually Ctrl+C or Ctrl+Q)

Or, if you don't have input access to the process, identify its PID and kill it :

On Windows :

C:\>Taskkill /PID <PID> /F

On Linux :

$>kill -SIGTERM <PID>
Antoine Auffray
  • 1,283
  • 1
  • 17
  • 33
  • 2
    I work on a Windows machine in a cygwin bash terminal. I tried Ctrl-C now but that doesn't work. – user1837293 Aug 07 '17 at 11:41
  • ps shows: PID PPID PGID WINPID TTY UID STIME COMMAND 6652 1 6652 6652 ? 197612 19:52:49 /usr/bin/mintty 1092 1 1092 1092 ? 197612 Jul 25 /usr/bin/mintty 11092 6652 11092 10060 pty1 197612 19:52:49 /usr/bin/bash 13868 1092 13868 992 pty0 197612 Jul 25 /usr/bin/bash 11428 13868 11428 17340 pty0 197612 12:48:27 /usr/bin/ps 11672 1092 1092 11672 ? 197612 Jul 25 /usr/bin/mintty Don't see the app there? – user1837293 Aug 07 '17 at 11:45
  • Now I terminated al bash terminals and I still get the message that I cannot start because 'Something is already running on port 3000.' How to solve? – user1837293 Aug 07 '17 at 11:58
  • Found Worker pid:4064 https://drive.google.com/drive/serviceworker.js?ouid=u8fe2c40ee7bb618a when running chrome://inspect/#service-workers following this post: https://github.com/react-boilerplate/react-boilerplate/issues/373 terminated that serviceworker in Chrome, however still the same error message on npm start – user1837293 Aug 07 '17 at 12:11
  • Next attempt: from the same post: tried netstat -ao which shows a list of active connections with their process number. Shows for connection on port 3000: TCP 0.0.0.0:3000 TAB-NAA:0 LISTENING 8528 Then do kill -SIGTERM 8528 However that returns error '-bash: kill: (8528) - No such process' ???? – user1837293 Aug 07 '17 at 12:19
  • 1
    Well finally: running this outside the cygwin bash terminal, in a (native) windows terminal (command prompt) did it: taskkill /PID 8528 – user1837293 Aug 07 '17 at 12:27
24

Add this in your package.json:

"stop": "taskkill -F -IM node.exe"
BrownRecluse
  • 1,638
  • 1
  • 16
  • 19
16

Hitting Ctrl + C will stop the running app after you provide an answer as Y as it asks; No need to close your terminal.

Satish Patel
  • 1,784
  • 1
  • 27
  • 39
  • 3
    CTRL+C does not ask anything and always leaves the process running until the terminal is closed, at least not in ZSH. – Rodney S. Foley Aug 05 '20 at 17:13
  • 2
    Same here. I need to close/re-open my terminal every time. This has only been an issue in the past month or two. – Zip184 Aug 28 '20 at 11:08
  • @Zip184 same here. No idea why, but without any changes to the config of a fairly old project or the terminal it runs in, CTRL+C has suddenly stopped working consistently for me. About 30% of the time the process stays running in the background. Weird and frustrating! – rococo Dec 07 '21 at 08:16
10

I had same issue too. I used this code to stop it

taskkill -F -IM node.exe

Just type the code in the terminal

ABODE
  • 958
  • 2
  • 15
  • 13
7

To make sure the process it finished just type the command:

$ killall -9 node

Will kill all processes by with the name "node". -9 to use kernel for killing the process, instead of process itself.

See the manpage

Syscall
  • 19,327
  • 10
  • 37
  • 52
brugobi
  • 259
  • 3
  • 5
3

I am having the same problem on Mac with a terminal started in VS code.

CTRL C kills the node server however a vscode process remains attached to the port afterwards and prevents restarting on the same port.

The following workaround works for me on Mac

npx kill-port 3000

You can call this from another terminal and it should kill the node server and any other related vscode processes attached to the port and allow you to restart the server on the same port.

You can also add a script in package.json:

"stop": "npx kill-port 3000"

Then call yarn stop to stop your server

user1650613
  • 137
  • 1
  • 3
2

If you're using Git Bash you might get an invalid arguments error. You have to use the following syntax.

To check which PID to kill:

netstat -aon

Look for 127.0.0.1:3000 under Local Address and note the PID

To kill the process:

taskkill -f //PID ####

where #### is the PID from above.

canvasD
  • 21
  • 2
2

You can try ctrl+z
After that type npx kill-port <PORT>
Usually react start on port 3000, but you better check it out

Le Chat
  • 21
  • 5
1

Simply use Ctrl + c and it will stop the server, simple.

Dilip Gautam
  • 114
  • 1
  • 4
1
  1. Open Task manager (Task Bar Righ click/press Ctrl + Alt + Delete)

  2. Go on to the Processes tab

  3. Find the Node. js: Server-side JavaScript

  4. End the task.

0

working in macOS but cant work ctrl + c

  1. open a terminal in vs code

  2. right-click on the terminal OR click on right top zsh-appname

  3. click last one kill terminal

Sarthak Raval
  • 1,001
  • 1
  • 10
  • 23
0

Step by step answer (Windows only; sorry!)...

Step 1: find processes using the port (port 3000 will be used for the examples here).

netstat -aon | findstr :3000

Output will be something like this:

  TCP    0.0.0.0:3000           0.0.0.0:0              LISTENING       275368
  TCP    127.0.0.1:3000         127.0.0.1:52462        ESTABLISHED     275368
  TCP    127.0.0.1:52462        127.0.0.1:3000         ESTABLISHED     15760

Step 2: identify the process ID.

We only want tasks which:

  1. Are TCP (first column), and
  2. Show port 3000 (second column), and
  3. Are LISTENING (fourth column).

In the output above that's line #1, which is process ID 275368 (last column).

Step 3: kill the process.

Use taskkill, with the f (forceful) flag, to end the process ID from Step 2:

taskkill -f -pid 275368

The taskkill flags are not case sensitive, and they can be set off with a dash or a slash, so these are all equivalent: taskkill -f -pid 275368, taskkill -F /PID 275368, taskkill /F /pid 275368, etc.

Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69