Autorestart nodejs app with supervisor on error and changes

This howto will show how to restart automatically a nodejs app on crash or on file changes using supervisor and nodemon.

Autorestart on changes

To install nodemon to autorestart app when you change app files:

nodemon

To test it’s working, use nodemon like node, passing all parameters you would pass to node:

Autorestart on errors

To install supervisor on a Debian-based system to restart app on crashes:

supervisor

Then create a supervisor-app-run.sh wrapper script on a custom location to monitor:

Now set up the config file for supervisor creating a new file on /etc/supervisor/conf.d/myapp.conf with:

Now you have to reread to apply changes without restarting all other services:

So in case of errors you got something like:

ERROR: CANT_REREAD: Invalid user name fakeuserhere in section ‘program:myapp’ (file: ‘/etc/supervisor/conf.d/myapp.conf’)

On success:

myapp: available

If you’ve changed the app configuration, you have to:

To apply, then restart the specific app.

Keep an eye on supervisor processes with:

Results:

Control the processes

Since exec was specified in the wrapper script before, supervisor can stop the node app on demand with:

Then supervisorctl status will display something like this:

To run again:

https://web.archive.org/web/20190502000000*/https://nodemon.io/

https://web.archive.org/web/20190502000000*/https://supervisord.readthedocs.io/

https://web.archive.org/web/20190502000000*/https://bash.cyberciti.biz/guide/Exec_command

Proxied content from gemini://chirale.org/2019-05-02_5539.gmi (external content)

Gemini request details:

Original URL
gemini://chirale.org/2019-05-02_5539.gmi
Status code
Success
Meta
text/gemini; charset=utf-8
Proxied by
kineto

Be advised that no attempt was made to verify the remote SSL certificate.