Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interrupt all lerna --parallel tasks at once

I have a lerna script (lerna dev) that boots up several package's dev servers with the --parallel option (if I didn't use that option, only the first service would start, but not the others). These servers serve their respective apps in dev mode on different ports, with hot reload. Basically, this allows smooth development, as we only have to enter one command to start working on several packages.

The problem I've noticed is that when I interrupt this lerna task, servers don't get shut down. When I run my lerna dev command, it prints messages explaining servers are already running on the ports they use. What this means is, when I shutdown the lerna dev command (with CTRL+C), it doesn't kill all of those running processes (some are killed, some aren't).

Interestingly enough, those that don't shutdown are create-react-app projects.

So here's my question: how do I make sure processes started via the lerna run command with the --parallel option are all killed alongside the main process?

PS: this happens on Unix systems, we don't use Windows.

like image 681
Gabriel Theron Avatar asked Oct 22 '18 09:10

Gabriel Theron


2 Answers

You could use kill-port to kill all the processes at once in an NPM script.

You could also add a script that both kills the ports and runs lerna dev to make sure the ports were closed and you don't get errors when the servers start.

"scripts": {
  "kill-ports": "kill-port --port 4444,5555,3000",
  "dev": "npm run kill-ports && lerna run dev --parallel"
}
like image 173
Dan Webb Avatar answered Oct 31 '22 21:10

Dan Webb


I would suggest that you don't use ctrl+c for this. Take a look at killing the process through the pid (process ID) using kill or pkill -f.

First, take a look at which lerna processes are running. My guess is that ps aux | grep lerna should display what you want there (tweak grep if needed). You may see a master process (I do when using Nginx, I've never used lerna) if so take the PID and type kill PID where the PID is your master PID. If this does not kill all the processes use pkill -f lerna to kill all processes that match the lerna search term (again tweak if needed).

For more information on how to kill the processes based on a search term see How to kill all processes matching a name

like image 28
Jeff Avatar answered Oct 31 '22 21:10

Jeff