Puppeteer don´t run with cronjobs
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:4
- Comments:15
Top GitHub Comments
I have the same issue and finally I decided to use PM2
One command and you’re set:
And you would need to run this one too to execute PM2 on startup:
I hope PM2 doesn’t overload my Raspberry Pi, that’s why I really wanted to use
crontab
initially 😃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 thecrontab -e
SHELL
andPATH
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
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 bashResources used: