
# `daemonize(options, callback)`

Control the lifecycle of a service with "start", "stop" and "status" options.

## Options

*   `cmd`   
    Command to daemonize.   
*   `name`   
    Service name for display purposes.   
*   `destination`   
    Path to the new script, use underscored name if destination is a directory.   
*   `cwd`   
    Set the process current working directory, "true" map to the destination directory.   
*   `detach`   
    Detached the process with `nohup`.   
*   `ssh` (object|ssh2)   
    Run the action on a remote server using SSH, an ssh2 instance or an
    configuration object used to initialize the SSH connection.   

## Callback parameters

*   `err`   
    Error object if any.   
*   `created`   
    Indicates if the destination script was created or modified.   

## Detach example

```js
mecano.daemonize({
  source: '/etc/',
  destination: 'node-sigar.tgz'
}, function(err, downloaded){
  console.log(err ? err.message : 'File was downloaded: ' + downloaded);
});
```

## Source Code

    module.exports = (options, callback) ->
      # Validate parameters
      throw Error "Required option 'destination'" unless options.destination
      destination = options.destination
      options.name ?= path.basename options.destination
      cwd = options.cwd
      cwd = 'cd $( dirname "${BASH_SOURCE}" )' if options.cwd is true
      cwd = "cd \"#{cwd}\"" if typeof cwd is 'string'
      detach = options.detach
      detach = ' &' if detach is true
      do_destination = ->
        fs.stat options.ssh, destination, (err, stat) ->
          return callback err if err
          return do_write() if stat.isFile()
          destination = path.join destination, "#{string.underscore options.name}"
          do_write()
      do_write = ->
        pid_file = options.pid_file
        pid_file ?= "/var/run/#{path.basename destination}.pid"
        out_file = options.pid_file
        out_file ?= "/var/log/#{path.basename destination}/#{path.basename destination}.out"
        err_file = options.pid_file
        err_file ?= "/var/log/#{path.basename destination}/#{path.basename destination}.err"
        mecano.write
          content: """
            #!/bin/bash

            #{cwd}

            name='#{options.name}'
            cmd='#{options.cmd}'
            pid_file="#{pid_file}"
            out_file='#{out_file}'
            err_file='#{err_file}'

            function start {
              if running; then
                echo "$name already started"
              else
                $cmd </dev/null >$out_file 2>$err_file #{detach}
                echo $! > $pid_file
                echo "$name started [`cat $pid_file`]"
              fi
            }

            function stop {
              if running; then
                kill $(<$pid_file) 2>/dev/null
                echo "$name stopped"
              else
                echo "$name already stopped"
              fi
            }

            function kill {
              if running; then
                kill -9 $(<$pid_file) 2>/dev/null
                echo "$name stopped"
              else
                echo "$name already stopped"
              fi
            }

            function status {
              if running; then
                pid=`cat $pid_file`
                echo "$name started [$pid]"
                return 0
              else
                echo "$name stopped"
                return 3
              fi
            }

            function running {
              if [ -f $pid_file ]; then
                pid=`cat $pid_file`
                if kill -0 >/dev/null 2>&1 $pid; then
                   return 0
                fi
                rm -f $pid_file
              fi
              return 1
            }

            function help {
              echo ""
              echo "$(basename "${BASH_SOURCE}") [help|start|status|stop]"
              echo ""
              echo "Manage the $name service."
              echo ""
              echo "help      Print this message"
              echo "start     Start the server"
              echo "status    Report the server status"
              echo "stop      Stop the server"
              echo "kill      Force the server to stop"
              echo ""
            }

            RETVAL=0
            case "$1" in
              start)
                start
              ;;
              stop)
                stop
              ;;
              restart)
                start
                stop
              ;;
              status)
                status
                RETVAL=$?
              ;;
              *)
                help
              ;;
            esac
            exit $RETVAL
            """
          destination: options.destination
          ssh: options.ssh
          log: options.log
          eof: true
        , callback
      do_destination()

## Dependencies

    path = require 'path'
    fs = require 'ssh2-fs'
    string = require './misc/string'

