Search code examples
node.jsshchild-processforever

How to run sh script from node.js project?


My sh script suddenly closes. I'm trying to control sh process by another script. Nohup doesn't help me so I decided to use my Node.js working on forever. So I have found child_process lib but doesn't know how to run sh script on it.


Solution

  • From your comment under your question I assume what you want is this:

    const { exec } = require('child_process')
    
    exec('path/to/your/specific/file.sh', (err, stdout, stderr) => {
        // do whatever you want
    });
    

    The path can be relative or absolute and the file must be executable.

    Another way would be to explicitly call sh.

    const { exec } = require('child_process')
    
    exec('sh path/to/your/specific/file.sh', (err, stdout, stderr) => {
        // do whatever you want
    });