Skip to content

Instantly share code, notes, and snippets.

Last active February 20, 2020 11:30
Show Gist options
  • Save jmervine/5873934 to your computer and use it in GitHub Desktop.
Save jmervine/5873934 to your computer and use it in GitHub Desktop.
Node.js forever Makefile tasks.
# Tasks
# - start :: starts application using forever
# - stop :: stops application using forever
# - restart :: restart application using forever
# This set's your local directory to to your NODE_PATH
# This is for local (non `-g`) npm installs.
# NODE_MODS = ./node_modules/.bin/
# Some good `forever` options.
FOREVER_OPTS = -p ./logs \
-l server_out.log \
-o ./logs/server_out.log \
-e ./logs/server_err.log \
--append \
--plain \
--minUptime 1000 \
--spinSleepTime 1000
start: setup/dirs
# starting app in server mode
$(NODE_EXEC) $(NODE_MODS)forever $(FOREVER_OPTS) $@ server.js
# stopping app in server mode
$(NODE_EXEC) $(NODE_MODS)forever $(FOREVER_OPTS) $@ server.js
restart: setup/dirs
# restarting app in server mode
$(NODE_EXEC) $(NODE_MODS)forever $(FOREVER_OPTS) $@ server.js
# creating required directories for `forever`
mkdir -p logs pids
Copy link

Using this Makefile how to start the server?

Copy link

I'm wondering the same. I'm a fan of forever, trying to figure out clustering and at the moment deploying to, but preparing for a move to AWS and I've never implemented a MakeFile myself despite having worked with them in compiling others' code for > 15 years :)

That said, I started Googling and quick figured out that "start", "stop", "restart", etc.. in this file are "targets". If this file exists in a directory by the name "Makefile", then you can execute "make" by itself and if the make utility is installed, it will look at your working directory for "Makefile" and execute it. You could just as easily name this file "MyApp" then execute "make MyApp" and it would do the same from what I can tell. I'm not 100% sure there and in any case, I can't find a clear and simple example of how to use the targets which seems pretty silly since they're clearly a CORE part of this concept. That said, I did find an example discussing use of "Phony" targets which provided enough detail for me to infer that a "Makefile" with a "phony" target of "clean" would be executed with "make clean". That leads me to believe that if there is a "Makefile" in a directory, then you execute "make sometarget" and the target inside "Makefile" will be executed. The important bit here is that the file must be called "Makefile".

If all that is right, then you put this file in your project root as "Makefile" and your package.json "start" script would then be "make start". Notice that the first line in the start target is "setup/dirs" which is itself a target that will create the logs and pids folders. From there sh expansion is used to run node to start forever to launch the app. Now I'm going to give that a try and report back if I find anything clearly wrong with what I've proposed above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment