Skip to content

Commit

Permalink
TERM,INT,QUIT,KILL
Browse files Browse the repository at this point in the history
  • Loading branch information
frodwith committed May 18, 2011
1 parent 57689a4 commit 2572508
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
5 changes: 3 additions & 2 deletions README.markdown
Expand Up @@ -59,8 +59,9 @@ some sort of stringifiable error object. Defaults to init.startFailed.


### init.stop(pidfile, cb) ### init.stop(pidfile, cb)


Stop your service. cb will be called after the process is killed, and defaults Sends your service TERM, INT, QUIT, in that order (with 2 second delays) and
to init.stopped. If the process was running, cb's first argument will be true. then KILL until the process is no longer running, then calls cb (defaults to
init.stopped). If the process was running, cb's first argument will be true.


### init.status(pidfile, cb) ### init.status(pidfile, cb)


Expand Down
11 changes: 10 additions & 1 deletion examples/test.coffee
Expand Up @@ -5,7 +5,16 @@ snorlax = ->
console.log if snore then "Snore..." else "Lax..." console.log if snore then "Snore..." else "Lax..."
snore = not snore snore = not snore



start = ->
interval = setInterval snorlax, 4000
process.on 'SIGTERM', -> console.log 'Mmm,mm.'
process.on 'SIGINT', -> console.log 'Mmmm!.'
process.on 'SIGQUIT', ->
console.log 'Mrpmph!'
clearInterval interval

init.simple init.simple
pidfile : './test.pid' pidfile : './test.pid'
logfile : './test.log' logfile : './test.log'
run : -> setInterval snorlax, 4000 run : start
16 changes: 9 additions & 7 deletions init.coffee
Expand Up @@ -71,15 +71,17 @@ exports.stopped = (killed) ->
exports.stop = (pidfile, cb = exports.stopped) -> exports.stop = (pidfile, cb = exports.stopped) ->
exports.status pidfile, ({pid}) -> exports.status pidfile, ({pid}) ->
if pid if pid
n = 0 signals = ['TERM', 'INT', 'QUIT', 'KILL']
do -> tryKill = ->
signal = if n > 3 then 'SIGKILL' else 'SIGTERM' sig = "SIG#{ signals[0] }"
try try
process.kill pid # throws when the process no longer exists
n += 1 process.kill pid, sig
setTimeout arguments.callee, 1000 signals.shift() if signals.length > 1
setTimeout (-> tryKill sig), 2000
catch e catch e
fs.unlink pidfile, -> cb(n > 0) fs.unlink pidfile, -> cb(signals.length < 4)
tryKill()
else else
cb false cb false


Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -12,7 +12,7 @@
"name" : "Paul Driver" "name" : "Paul Driver"
} }
], ],
"version" : "0.1.0", "version" : "0.1.1",
"licences" : [ "licences" : [
{ {
"url" : "http://www.opensource.org/licenses/mit-license", "url" : "http://www.opensource.org/licenses/mit-license",
Expand Down

0 comments on commit 2572508

Please sign in to comment.