3

currently I'm running a Puppeteer script (in an hourly interval) in a docker container on a EC2 instance. Bellow is the code (Thank you Edi Imanto if you are reading this)!

const cron = require('node-cron')
const execute = require('child_process').exec
const puppeteer = require('puppeteer')
const processIDs = []

const createBrowser = async () => {
    const browser = await puppeteer.launch()
    processIDs.push(browser.process().pid)
    return browser
}

const closeBrowser = async (browser) => {
    await browser.close()
    for (let i = 0; i < processIDs.length; i++) {
        execute(`kill -9 ${processIDs[i]}`)
    }
}


const runHourly = async () => {
    const browser = await createBrowser()    
    const await = doSomething()  

    const closeTab = await page.close()
    const exit = await closeBrowser(browser)
}

cron.schedule('* 0/1 * * *', () => {
    await runHourly()
})

When I look at HTOP there are so many "chrome" tasks and the numbers are increasing every hour which means that they are not closing as intended. enter image description here

console.log-ing the proccessIDs[i] that it is killing, I can't really find correspondence to the PIDs in HTOP and I assume that this is because the script is running on a container(?). I'm not sure then how to properly kill these tasks - some help/advice would be really appreciated.

Another thought, could it be that I didn't execute the kill without sudo?

Thanks again!

Bellow is the docker file

FROM node:10.17.0-slim@sha256:17df3b18bc0f1d3ebccbd91e8ca8e2b06d67cb4dc6ca55e8c09c36c39fd4535d

RUN  apt-get update \
  && apt-get install -y python \  
  && apt-get install -y build-essential \ 
  && apt-get install -y wget --no-install-recommends \
  && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
  && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
  && apt-get update \
  && apt-get install -y google-chrome-unstable --no-install-recommends \
  && rm -rf /var/lib/apt/lists/* \
  && wget --quiet https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh -O /usr/sbin/wait-for-it.sh \
  && chmod +x /usr/sbin/wait-for-it.sh 

USER root

# Install Puppeteer under /node_modules so it's available system-wide
WORKDIR /usr/src/app 
COPY .  . 
ADD package.json package-lock.json /
RUN npm install

CMD ["node", "transfer.js"]

1 Answers1

2

You need to reap zombie processes. Please follow the documentation troubleshooting guide for running Puppeteer in Docker. In a nutshell, you need to run the container with --init option to start a simple process as PID 0 that will take care of reaping chromium instances after they become detached from Puppeteer.

zr0gravity7
  • 2,917
  • 1
  • 12
  • 33