Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to exit/close/kill dev server #12419

Answered by Jaarson
Jaarson asked this question in Q&A
Discussion options

Hi!

Sometimes vite dev server hangs 😑 OK, not a big deal, but the port is already taken and starting new server assigns a new port number.
How can I kill dev server, so I can restart it and keep using my preferred port?

You must be logged in to vote

OK, just figured it out. In terminal:

ps -ax | grep vite

then using PID number and -9 for force kill

kill -9 <PID>

Replies: 5 comments 1 reply

Comment options

OK, just figured it out. In terminal:

ps -ax | grep vite

then using PID number and -9 for force kill

kill -9 <PID>
You must be logged in to vote
0 replies
Answer selected by Jaarson
Comment options

I stumbled upon this because I was having trouble taking down a Vite dev server in an Sbt task. The Vite Dev server is a bit finicky, and may stay alive even after killing the parent process. That is, if you start vite with something like this (or an equivalent program in a different programming language):

import cp from 'node:child_process';
console.log("Spawning Vite Dev server...");
const p = cp.exec('npx vite');
p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stderr);
console.log("Shutting down in 5 seconds");
await new Promise((resolve) => setTimeout(resolve, 5000));
console.log("Shutting down Vite Dev server...");
p.kill();

Calling kill won't destroy the server worker process. Not even trying to use the terminal UI by sending "q\n" to stdin will make a difference.

The only way that allowed me to have full control of the dev server's lifetime was to spawn a custom script which uses the Vite API directly and actively detects whether it should stop. For instance, if I want this server to live for as long as there is another server listening on local port 9000:

import { createServer } from 'vite';
import net from 'node:net';
import url from 'node:url';
console.log("Spawning Vite Dev server...");
const server = await createServer();
await server.listen(5173);
server.printUrls();
const checkPlayServer = async () => {
 // check if the server is still alive
 let socket = net.connect({
 port: 9000,
 host: 'localhost',
 });
 socket.on('error', async (err) => {
 console.log("Closing Vite Dev server");
 socket.destroy();
 await server.close();
 })
 socket.on('connect', () => {
 socket.end();
 // connection still OK, resuming pinging
 setTimeout(checkPlayServer, 2_500);
 })
};
setTimeout(checkPlayServer, 2_500);
You must be logged in to vote
0 replies
Comment options

just use this command:
npx kill-port 5173

You must be logged in to vote
0 replies
Comment options

npx vite then q + enter worked for me

You must be logged in to vote
1 reply
Comment options

➜ press h + enter to show help
h

Shortcuts
press r + enter to restart the server
press u + enter to show server url
press o + enter to open in browser
press c + enter to clear console
press q + enter to quit

For anyone else who finds this and wonders what other commands are supported. 😊

Comment options

Kill the process and all of its children: pkill -TERM -P [PID]
example:

if yarn vite > /dev/null; then
 pid=$!
 // do something else
 if ! pkill -TERM -P $pid; then echo "UNABLE TO KILL THE PROCESS! PID: $pid"
fi

Generally this is not needed, as most server software worth its salt has a "stop" command to prevent the user from ungracefully terminating the server process. I could not find any documentation for Vite detailing such a mechanism. It would seem that the developers for Vite have decided that a SIGINT is enough.... which if you've worked with servers before you'd know is not always the case.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

AltStyle によって変換されたページ (->オリジナル) /