💾 Archived View for chirale.org › 2019-05-02_5539.gmi captured on 2024-05-12 at 15:07:27. Gemini links have been rewritten to link to archived content

View Raw

More Information

-=-=-=-=-=-=-

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

 npm install -g nodemon 

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

 nodemon app.js --myoptionalparameter MYVALUE; 

Autorestart on errors

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

supervisor

 sudo apt-get install supervisor 

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

 #!/bin/bash cd /path/to/my/app; exec node app.js --myoptionalparameter MYVALUE; 

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

 [program:myapp] directory=/path/to/my/app command=bash /path/to/my/supervisor-app-run.sh priority=10 ; the relative start priority (default 999) autostart=true ; start at supervisord start (default: true) autorestart=true ; retstart at unexpected quit (default: true) ; startsecs=-1 ; number of secs prog must stay running (def. 10) ; startretries=3 ; max # of serial start failures (default 3) exitcodes=0,2 ; 'expected' exit codes for process (default 0,2) stopsignal=QUIT ; signal used to kill process (default TERM) ; stopwaitsecs=10 ; max num secs to wait before SIGKILL (default 10) user=USER_TO_RUN_APP_HERE ; setuid to this UNIX account to run the program log_stdout=true ; if true, log program stdout (default true) log_stderr=true ; if true, log program stderr (def false) logfile_maxbytes=10MB ; max # logfile bytes b4 rotation (default 50MB) logfile_backups=10 ; # of logfile backups (default 10) 

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

 sudo supervisorctl reread 

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:

 sudo supervisorctl update 

To apply, then restart the specific app.

 sudo supervisorctl restart myapp 

Keep an eye on supervisor processes with:

 sudo supervisorctl status 

Results:

 myapp RUNNING pid ****, uptime 0:00:59 anotherapp RUNNING pid ****, uptime 0:29:33 

Control the processes

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

 sudo supervisorctl stop myapp 

Then supervisorctl status will display something like this:

 myapp STOPPED Apr 27 22:53 AM anotherapp RUNNING pid ****, uptime 0:28:16 

To run again:

 sudo supervisorctl start myapp 

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