9

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:

  • Use an absolute path for the working directory
  • checked if python3 is in PATH: which python3 returns /usr/bin/python3
  • run the node server with sudo
  • debug logged 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?

asked Oct 2, 2018 at 18:01
5
  • 1
    What exactly is that remote server? /bin/sh is missing, so it's not a regular Linux server I'd say. Commented Oct 2, 2018 at 18:03
  • it is a regular cloud server running Ubuntu 18.04 Commented Oct 2, 2018 at 18:04
  • 1
    So you can log in on that server and execute /bin/sh yourself? You could try to use the shell option for child_process.exec so it points directly to the Python executable, but it seems strange that a regular Linux server doesn't have /bin/sh Commented Oct 2, 2018 at 18:06
  • /bin/sh -c ls shows the folder contents, so yeah, it does exist Commented Oct 2, 2018 at 18:08
  • 1
    Ruling out an invalid shell, the only other reason I can think of that will cause that error is an invalid cwd, but you already ruled that out too. So still strange. Commented Oct 2, 2018 at 18:14

1 Answer 1

12

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...

answered Oct 2, 2018 at 18:18
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.