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
- Copy the script “as is” to a local disk.
- The script has this associative array, which contains the root folder names of all cPanel-hosted Node.js apps:
Edit the array in your preferred editor so that it fits your specific web hosting setup.declare -A domains domains[1]='mydomain.com' domains[2]='api.mydomain.com' domains[3]='dev.mydomain.com' domains[4]='test.mydomain.com'
- Verify that SEARCHSTRING actually contains what you want the script to look for:
SEARCHSTRING="lsnode:/home/$USER/"
- 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
- Adjust LOGDIR if you want – it is currently set to log in the script’s folder.
- Create a folder on your web host to store your own scripts – for example:
/cronscripts
. - Copy the customized
cleanup_orphans.sh
to the web host folder:/cronscripts/cleanup_orphans.sh
. - 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
- 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