Search code examples
ubuntuserviceupstart

Proper way to make a node app a service


I have a project folder with the app's contents, and to start it manually, I run nohup npm start & and then disown the proc.

I would like to figure out a way to make this a service mynodeapp {start|stop|restart|etc} command instead.

I am also aware that Ubuntu (and Debian in general) is deprecating the SysV init scripts and recommend upstart configs instead in /etc/init/*.conf.

How would I go about writing an upstart config with start-stop-daemon?

Thanks.


Solution

  • I'll answer this myself with the solution that worked best for me.

    After digging around a bit, I just decided to undertake the burden of reading the upstart docs, and came up with the following in /etc/init/myapp.conf:

    # Node.js app config
    
    description     "Node.js app upstart config"
    author          "George K."
    
    # Starting conditions
    start on (local-filesystems and net-device-up IFACE=eth0)
    
    # Stopping conditions
    stop on shutdown
    
    # If parent or any child exits, respawn
    respawn
    
    # The deed
    script
        export HOME="/root"
        export WORKER_COUNT=4
    
        exec /usr/local/bin/node /path/to/app >> /var/log/app.log 2>&1
    end script