Run Multiple Make Targets Concurrently
make is a widely used and powerful tool for running common tasks in a project.
I often look at the Makefile
of a project in order to see how a project perform
regular tasks such as running tests, compiling and starting db, etc.
Use Case 1
I want to run 2 local servers at the same time. I can run them on separate terminal windows but I don’t want to do that. I’m a lazy developer and want to use one line solution.
For instance I want to run a python
backend server and javascript
front end
server for live reload
— if there is any change in static assets like js
,
css
, scss
etc..
# Makefile
django-server: ## Run the Django server
python manage.py runserver 8000
npm-server: ## Run npm server to live reload
npm run dev
You can run them concurrently like below:
$ make -j 2 npm-server django-server
Or you can use a target
for it as well:
# Makefile
servers:
make -j 2 npm-server django-server
django-server: ## Run the Django server
python manage.py runserver 8000
npm-server: ## Run npm server to live reload
npm run dev
Then run make servers
.
Use Case 2
I want to run a local server and open a browser related to it. I don’t want to
manually click my browser and type localhost:$PORT
.
Solution:
# Makefile
local-dev: django-server browser
django-server: ## Run the Django server
python manage.py runserver 8000
browser:
google-chrome --new-window http://localhost:8000
# or if you use firefox
# firefox --new-window localhost:8000
Then type make local-dev
.
All done.