Script for cleaning up Node.js processes that lock your web hosting

This script should run as a cron job on your cPanel web hosting account.

This is not the perfect solution (since none exists) – it is rather a temporary workaround, as it does not solve the actual problems. What the script does is handle the symptoms (hanging processes) by sending them a SIGTERM signal to make them terminate – and thereby release locked resources – which creates stability around the NodeJS app hosted on the web host.

Purpose
Running NodeJS applications in a cPanel-hosted environment is a bit different from running NodeJS apps locally. Processes can fail and cause cPanel’s process control system to restart a new NodeJS instance – or processes that should have ended may remain stuck as “ghost processes”. Eventually, this can consume all resources until cPanel’s Resource Monitor blocks further use.

cleanup_orphans.sh [options] 'SEARCHSTRING'

    Options:
    -v    Verbose
    -d    Dry-run - show intended behaviour but don't touch anything
    -f    Force kill - use SIGKILL instead of SIGTERM
    -h    Help (this usage message)

    Default SEARCHSTRING: 'lsnode:/home/$USER/'

    If called without 'SEARCHSTRING', the script runs through the internal 
    array of domains (foldernames added to search string), to clean up each
    'domain' separately, one by one.

If the script is called with 'SEARCHSTRING' as input, the predefined SEARCHSTRING will be replaced with the provided input.

Otherwise, the script will iterate through the internal array of domains (folder names added to the search string) to clean up each “domain” separately, one at a time.

Logging

Logging is implemented to have a history – so you can track how many processes are being handled. At the same time, too much logging can create a new problem; we do not want the disk to run out of space due to endless logging. The solution is to limit the logging to two files, each of which can contain up to 1000 lines.

Implementation in a specific cPanel solution.

Implementation in a specific cPanel solution

  1. Copy the script “as is” to a local disk.
  2. The script has this associative array, which contains the root folder names of all cPanel-hosted Node.js apps:
    declare -A domains
    domains[1]='mydomain.com'
    domains[2]='api.mydomain.com'
    domains[3]='dev.mydomain.com'
    domains[4]='test.mydomain.com'
    Edit the array in your preferred editor so that it fits your specific web hosting setup.
  3. Verify that SEARCHSTRING actually contains what you want the script to look for:
    SEARCHSTRING="lsnode:/home/$USER/"
  4. Adjust the log size if you want. This is the part that handles log size and rotation of log files (I won’t go into details here):
    # rotate log - maintain logsize at an acceptable limit
    LOGSIZE=$(cat $LOGDIR/$LOGFILE | wc -l)
    if [ $LOGSIZE -gt 1000 ]; then
        mv -f $LOGDIR/$LOGFILE $LOGDIR/$LOGFILE.1
    fi
  5. Adjust LOGDIR if you want – it is currently set to log in the script’s folder.
  6. Create a folder on your web host to store your own scripts – for example: /cronscripts.
  7. Copy the customized cleanup_orphans.sh to the web host folder: /cronscripts/cleanup_orphans.sh.
  8. Add a cron job in cPanel. I suggest running the script regularly, e.g., every 5–30 minutes (depending on the amount of traffic that creates “orphaned” processes).
    Example:
    */5 * * * * /home/infoqrco/cronscripts/cleanup_orphans.sh
  9. Monitor the first run; try the flags -v (verbose) and -d (dry-run). Check that the log file is created, and verify that the cron job runs as expected.

Done! Your “orphaned” Node.js processes are now handled automatically.

Original guide created by one of our clever customers ;-) : https://github.com/jlmantov/shellscripts/blob/main/docs/cleanup_orphans.md

  • 0 Users Found This Useful
Was this answer helpful?

Didn't find a solution?

If you haven't found a solution by searching or navigating our categories, you can open a new support ticket.

Open New Ticket

Related Articles

How to fix 503 errors on your website

A 503 error occurs when there aren't enough resources to execute a script, causing the server to...

How to resolve 503 errors during data upload and import

503 Error: How to Handle Exceeding Server Limits A 503 error is a server issue that occurs when...

I can't access my own pages, but others can

If you find that you are unable to log in to cPanel with us or see your own pages while others...

How to limit the size of your error_log files

Every time a PHP error occurs on your site – whether it’s just a warning or an actual error – it...