Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node Error spawn /bin/sh ENOENT on remote server

Tags:

python

node.js

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?

like image 209
Taxel Avatar asked Oct 02 '18 18:10

Taxel


1 Answers

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

like image 190
Taxel Avatar answered Nov 06 '22 23:11

Taxel