38

Currently I initialise the firebase emulators with:

$ firebase emulators:start

After some time working on it I want to stop it. How can I then stop the emulators?

user1447414
  • 1,136
  • 2
  • 10
  • 24

9 Answers9

54
  1. Check which process is occupying the port sudo lsof -i tcp:<port>
  2. Kill the process kill -9 <process id>
xmkevinchen
  • 1,356
  • 13
  • 17
  • 2
    I have found that if you exit VSCode without shutting down the emulators first it can remain running and without the above commands, you will need to restart the computer to be able to restart the emulators. Thank you for sharing @xmkevinchen – ra9r Sep 19 '20 at 20:45
  • 3
    For windows: https://stackoverflow.com/a/39633428/681803 – ThdK Sep 21 '20 at 17:12
  • @ra9r on linux the process is called _java_. If you kill it you won't need to restart your computer. – perepm Jan 21 '21 at 09:14
  • 5
    I'm flabbergasted there is no shutdown command...this is an insane workflow. – Daniel Brotherston Apr 26 '21 at 12:06
  • 1
    This seems like such an obvious feature to include in a command `firebase emulators:stop` or similar, it makes me think I'm using the tool wrong. Is the normal workflow to start up the emulators and leave them running in the background while developing for long periods of time? – Jeff Neet Dec 14 '21 at 20:20
  • @JeffNeet that's the way I use it. I fire up the function/firestore emulators and work on event functions and db triggers. Sometimes it completely blows out and stops with an error leaving the process running which is when I need the answer below by user kazuwombat. Generally tho, 2x Ctrl-C is enough. – monsto Apr 19 '22 at 13:25
20

according to this: https://github.com/firebase/firebase-tools/issues/1367 Ctrl+C kills the emulators

15

If you don't want to have to check the port every time, you can stop command with below

kill -9 (lsof -t -i:5002 -i:5001)

(-i:xxxx are your running emulator ports in firebase.json.)

Moreover, I don't want to memorize this long command. So I made package.json script below.

"scripts": {
   ...
   "stop": "lsof -t -i :5001 -i:5002 | xargs kill -9",
   ...
}
kazuwombat
  • 1,249
  • 13
  • 18
10

One cross-platform solution is to just run: npx kill-port 4000,8080,8085

GorvGoyl
  • 33,155
  • 24
  • 179
  • 178
10

If you wanna kill all firebase emulators you can easily do that by firing this command

$ lsof -t -i:8080 -i:9000 -i:9099 -i:9199 -i:9090 | xargs kill -9

When you don't want to type this long command each time I advise you to use a script in the package.json file

 "scripts": {
   "emulators:start": "firebase emulators:start",
   "emulators:stop": "lsof -t -i:5001 -i:5002 -i:8080 -i:9000 -i:9099 -i:9199 -i:9090 | xargs kill -9"
  }

One for starting your emulators and one for stopping, in case Ctr+C didn't stop the processes in the background.

Those are the default PORTS from the documentation page in firebase. You should also check your firebase.json file and replace the PORTS in the previous command if they are different.

Alaeddine
  • 1,441
  • 2
  • 22
  • 44
4

I've tried all the answers above, and none does what I was expecting: to gracefully end the emulator suite as a whole without having to ctrl+c, leaving no ports occupied. Here is how I've solved it.

TLDR: lsof -ti :4400 | xargs --no-run-if-empty kill

The port being 4400, as it's the default port for the Emulator Hub. Although with this command you'll end the emulator regardless of the process you kill.

The"-9" flag used in the other answers does not send the SIGTERM signal to the process, but forcefully kills it instead. This prevents the emulator from ending gracefully.

perepm
  • 812
  • 8
  • 20
4

An alternative is to use firebase emulators:exec, which, according to the CLI documentation, does this:

start the local Firebase emulators, run a test script, then shut down the emulators

Since I put my test running command in a Makefile, I use the following command to test firestore from a Python firebase_admin SDK:

firebase emulators:exec "make test" --only firestore

The set-up and tear-down of the port is handled by firebase directly.

Fanchen Bao
  • 1,893
  • 1
  • 12
  • 25
3

So here's a bit of fun I just discovered. Double tap CTRL-C (hold down CTRL and double tap C) in the terminal running the emulator when you want to shut down the emulator and clear all the ports and processes.

I've checked it a couple of times and looked to see if the ports are free, they all are.

Just using CTRL-C once leaves you with all those ports still being used. Hope it solves the problem for others and not just me.

Edit: Looks to me now like the problem only persists until first shutdown of the computer after setting up the emulators. I now have no problems with the emulators shutting down properly with a single CTRL-C.

OhNoNotScott
  • 784
  • 2
  • 7
  • 12
1

For Windows, first find PID of the process using port 8080 by typing this in cmd.

netstat -a -n -o | find "8080"

Next, kill that process by:

taskkill /PID <type PID here>
deZak
  • 69
  • 1
  • 2