question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Puppeteer don´t run with cronjobs

See original GitHub issue

I want to run node.js process as a cron job, but process are not running well.

My javascript code is just the first puppeteer example available in docs (https://pptr.dev/):

const puppeteer = require('puppeteer'),
      process   = require('process')
;

(async () => {

    console.log( 'process owner: ' + process.env.USER );
    
    console.log('browser launch');
    const browser = await puppeteer.launch();
    
    console.log('browser launched, open new page');
    const page = await browser.newPage();
    
    console.log('Go to example.com');
    await page.goto('https://example.com');
    await page.screenshot({path: process.env.USER + '_example.png'});

    await browser.close();
})();

I’ve added username to validate the user that’s running the script and cron job is this:

01 * * * * /usr/local/nvm/versions/node/v10.16.0/bin/node /var/node/pupp_test/init.js > /var/node/pupp_test/output.txt

So, if I run manually the script:

process owner: efeferrari
browser launch
browser launched, open new page
Go to example.com

But, when script runs with cronjob, output file is just:

process owner: efeferrari
browser launch

I don’t understand why Puppeteer has this problem if the process owner is the same with bash and cron

I have node.js installed with NVM and configured Puppeteer with setup setuid sandbox, so I can run node and puppeteer with all users in the machine.

Searching about my problem, I’ve find in StackOverflow a topic talking about executing Puppeteer from PHP and I’ve already try with setfacl in node_modules/puppeter/.local-chromium/ and in /usr/local/sbin/chrome-devel-sandbox, but does not resolve my issue

  • OS is Centos 7
  • Node version is 10.16.0
  • Puppeteer version is 1.19.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:15

github_iconTop GitHub Comments

2reactions
alfonmgacommented, Oct 26, 2020

I have the same issue and finally I decided to use PM2

One command and you’re set:

pm2 start index.js --log app.log --time --cron "* * * * *" && pm2 save

And you would need to run this one too to execute PM2 on startup:

pm2 startup

I hope PM2 doesn’t overload my Raspberry Pi, that’s why I really wanted to use crontab initially 😃

1reaction
softwaredeveloptamcommented, Mar 19, 2021

Original Post Before using PM2, I’m still looking for a particular solution for this. If I find it, I’ll post it here.

Edit 1 Found an answer for myself that worked with crontab, will post it in a bit

Edit 2 After installing all the additional items to get Chromium and Puppeteer working together. I set up my cronjob as root user and I had to specific a couple of things within the crontab -e

SHELL and PATH need to be specify to get the proper environment variables as the crontab has it’s own environmental variables that differ from your host machine.

  • echo $PATH
  • Then whichever $PATH that was here, I inserted it below.
  • PATH=/root/bin (yours should be different or more lengthy, you can choose which ones are necessary. Between each : is a path to where your environment variables are)
  • SHELL=/bin/bash so it would have access to bash

Resources used:

Read more comments on GitHub >

github_iconTop Results From Across the Web

Puppeteer don´t run with cronjobs · Issue #4759 - GitHub
I want to run node.js process as a cron job, but process are not running well. My javascript code is just the first...
Read more >
Puppeteer don´t run with cronjobs - node.js - Stack Overflow
I want to run node.js process as a cron job, but process are not running well. My javascript code is just the first...
Read more >
Puppeteer, Cron Jobs, and You - Blog by Evan Cooper
A quick guide on how to run a puppeteer script in a cron job without doing a bunch of weird stuff with your...
Read more >
Create a Remote Jobs Automated Scraping Bot on ... - Medium
Let's first launch a browser on puppeteer and navigate to remoteok.io page. We will save all jobs in an array (You can create...
Read more >
How to Schedule a Job in Node Using node-cron
Step 4 - How to Implement the Cron Job. In this step, you will implement a simple cron job. Create a new JavaScript...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found