How can I shut down the local firebase emulators?
Asked Answered
S

13

58

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?

Salford answered 1/7, 2020 at 20:15 Comment(0)
S
75
  1. Check which process is occupying the port sudo lsof -i tcp:<port>
  2. Kill the process kill -9 <process id>
Scree answered 26/8, 2020 at 16:57 Comment(7)
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 @ScreeFibula
For windows: https://mcmap.net/q/35768/-how-do-i-remove-the-process-currently-using-a-port-on-localhost-in-windows-closedCody
@Fibula on linux the process is called java. If you kill it you won't need to restart your computer.Underestimate
I'm flabbergasted there is no shutdown command...this is an insane workflow.Banker
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?Referent
@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.Magnetostriction
Using kill -2 might be better as it will send SIGINT instead of SIGKILL. It will have the same effect as pressing Ctrl + C in the emulator terminal and will allow for a clean shutdown.Tokharian
T
32

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

Tidewater answered 28/4, 2021 at 15:12 Comment(4)
Nope. Doesn't work on a MacBook.Coracoid
Not possible if there isn't one visually running.Myo
Doesn't kill the java console with Ctrl+C in windows alsoCatchword
It gracefully shuts down on a Mac now; Ctrl+C initiates a graceful shutdown that takes about 3 seconds to complete (there's output in the console as it happens). If you press Ctrl+C again during the graceful shutdown, then some ports might stay blocked (due to Java keeping them) – in this case, refer to another answer that explains how to release ports.Turoff
C
32

One cross-platform solution is to just run kill-port :

npx kill-port 4000 8080 8085

Corvette answered 22/6, 2021 at 22:32 Comment(3)
Faster option, and work in windows and LinuxMaggio
The command should be: npx kill-port 4000 8080 8085. Spaces between port numbers instead of commas.Forego
package health is low snyk.io/advisor/npm-package/kill-portConquistador
A
27

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.

Alternation answered 10/12, 2021 at 20:22 Comment(3)
add port 5000 for firebase hostingBinny
Thanks, there are multiple emulators you can check the documentation and add what you needAlternation
where is the package.json file ?Michelsen
O
18

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",
   ...
}
Overbold answered 4/11, 2020 at 5:27 Comment(1)
Nothing like a little automation to simplify your day.Magnetostriction
P
6

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.

Pul answered 30/5, 2021 at 2:18 Comment(0)
V
6

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.

Valerlan answered 23/10, 2021 at 7:36 Comment(0)
U
5

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.

Underestimate answered 21/1, 2021 at 9:40 Comment(1)
this was it for me, though --no-run-if-empty is bogus on macOSForehand
J
3

If you're using Node in your project you can call

npx kill-port 3000 8080 9000 5001 5000 9199

You can put this into a script in your package.json to easily call with npm run kill.

"scripts": {
  "kill": "npx kill-port 3000 8080 9000 5001 5000 9199"
}
Jemmie answered 29/12, 2022 at 11:33 Comment(0)
P
3

Here's one way you can stop the emulators:

pkill -f "firebase/emulators"

According to the Firebase Emulators SDK documentation (link):

Calling emulators:start will download the emulators to ~/.cache/firebase/emulators/ if they are not already installed.

So, this solution works because:

  • The emulator process is provided a .jar file, located in this directory
  • Since this jar path is in the process name, pkill -f can find it by name

This is more robust than solutions that make assumptions about what ports the emulator is running on, and it works if you have multiple instances of a specific emulator running.

Planksheer answered 28/2, 2023 at 20:45 Comment(1)
This one works!Interne
L
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>
Liberati answered 11/10, 2021 at 8:16 Comment(0)
S
0

See source code commands emulators-start commands emulators-exec

Both use shutdownWhenKilled

export function shutdownWhenKilled(options: any): Promise<void> {
  return new Promise<void>((res, rej) => {
    ["SIGINT", "SIGTERM", "SIGHUP", "SIGQUIT"].forEach((signal: string) => {
      process.on(signal as Signals, processKillSignal(signal as Signals, res, rej, options));
    });
  }).catch((e) => {
    logger.debug(e);
    utils.logLabeledWarning(
      "emulators",
      "emulators failed to shut down cleanly, see firebase-debug.log for details.",
    );
    throw e;
  });
}

see processKillSignal for effect of double signal.

Seften answered 6/6, 2024 at 13:7 Comment(0)
L
0

I was tired of killing ports manually by using :

netstat -ano | findstr :<PORT>
taskkill /PID <PID> /F

Automate killing all ports (Windows)

  1. Create new file
  2. Paste this snippet
@echo off
setlocal

rem List of ports to kill
set PORTS=5001 8080 9000 9199

rem Loop through each port and kill the process using it
for %%P in (%PORTS%) do (
    echo Killing process on port %%P
    for /f "tokens=5" %%i in ('netstat -ano ^| findstr :%%P') do (
        echo Found PID %%i on port %%P
        taskkill /PID %%i /F
    )
)

echo All specified ports have been processed.
endlocal
pause
  1. save as .bat (i.e. portKiller.bat)

Usage

Now you can execute it by double click on file or run from CLI.
OR
add to npm scrip Below command works for powershell and cmd in my VSCode. Modify according to you system.
package.json

...
  "scripts": {
     ...
    "kill": "portKiller.bat"
  },
...

Note: replace the ports with yours. check firebase.json I have used default values from firebase.json:

...
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "database": {
      "port": 9000
    },
    "storage": {
      "port": 9199
    }
...
]
Luthanen answered 15/7, 2024 at 2:28 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.