-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
-
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?
Beta Was this translation helpful? Give feedback.
All reactions
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
-
OK, just figured it out. In terminal:
ps -ax | grep vite
then using PID number and -9
for force kill
kill -9 <PID>
Beta Was this translation helpful? Give feedback.
All reactions
-
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);
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1 -
🚀 1
-
just use this command:
npx kill-port 5173
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 13 -
🎉 2 -
🚀 2
-
npx vite
then q
+ enter
worked for me
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 6
-
➜ 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. 😊
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 6
-
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.
Beta Was this translation helpful? Give feedback.