Skip to content

Instantly share code, notes, and snippets.

@aral
Created December 25, 2013 12:08
Show Gist options
  • Save aral/8122688 to your computer and use it in GitHub Desktop.
Save aral/8122688 to your computer and use it in GitHub Desktop.
Using nginx for dev is much faster than grunt-contrib-connect (especially with a livereload workflow) but grunt-nginx doesn’t automatically shut down the server when exiting (e.g., on CTRL-C when watching, or if there is an exception). This little snippet will detect and shut down the server.
# e.g. set up nginx task
module.exports = (grunt) ->
grunt.initConfig
nginx:
options:
config: '/usr/local/etc/nginx/nginx.conf'
# …
# Make sure we shut down the nginx server in case it’s running.
exec = require('child_process').exec
cleanup = ->
console.log('')
console.log('=============================================')
console.log('Exiting gracefully. Shutting down the server…')
console.log('=============================================')
exec 'nginx -s stop', (error, stdout, stderr) ->
if error
console.log 'Couldn’t stop server: ' + error
console.log stdout
console.log stderr
process.exit()
process.on 'SIGINT', cleanup
process.on 'uncaughtException', cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment