Okay I know there are multiple questions regarding this topic, I read most of them and nothing helped.
I am trying to have a node server run a python script periodically. For this I've got the following code:
const
{ exec } = require('child_process');
const fs = require('fs');
let config = [
{
"title": "My Python Script",
"description": "A script that can scrape website data",
"command": "python3 collect_data.py",
"dir": "../file_folder",
"minutesBetweenExecution": 60
}
];
const intervals = [];
function startCronjobs() {
while (intervals.length > 0) {
clearInterval(intervals.pop());
}
console.log("Starting cronjobs...")
for (let i = 0; i < config.length; i++) {
const cron = config[i];
const func = () => {
exec(cron.command, { cwd: cron.dir, timeout: 40 * 60000 }, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.error('Command could not be executed. Error: ', err, ' cron entry: ', cron.title);
return;
}
console.log('Output after ', cron.title);
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
};
intervals.push(setInterval(func, parseInt(cron.minutesBetweenExecution) * 60000));
console.log('Started Cronjob', cron.title);
func();
}
}
startCronjobs();
As you can see, the script just runs exec with a specified command and working directory.
Yet, when trying to execute func()
node throws the error specified above.
Stuff I've tried to fix this:
python3
is in PATH: which python3
returns /usr/bin/python3
sudo
process.env.PATH
: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
The weird thing about this is: When I am trying to run this on my local linux machine, it works perfectly. On my remote server, however, it does not. What am I missing?
Okay, I am an idiot.
The folder on my local machine is named differently from the git repo and therefore the folder on the server, because I just git cloned it.
Fix: change _
in the working directory to -
and it works...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With