I wrote the following web server in node.js using express and hbs that can run shell files, but I keep getting the following error when I type this url into a web browser
linux username here is replaced with my linux username
http://127.0.0.1:3000/run?file="/home/linux username here/nasServer/GameServers/minecraft/1.16.2 server/run.sh"
stderr: bash: <path to .sh file here>: No such file or directory. child process exited with code 127.
Contents of nodejs file:
const express = require('express') const hbs = require('hbs') const app = express() const port = 3000 // Set up handlebars engine app.set('view engine', 'hbs') app.get('/run', (req, res) => { const { spawn } = require('child_process'); let callback = "" ls = spawn("bash", [req.query.file]) ls.stdout.on('data', function (data) { console.log('stdout: ' + data.toString()); }); ls.stderr.on('data', function (data) { console.log('stderr: ' + data.toString()); }); ls.on('close', function (code) { console.log('child process exited with code ' + code.toString()); }); res.send({ out: callback }) }) app.listen(port, () => { console.log(`App listening on port ${port}`) })
This is the contents of run.sh:
#!/bin/bash java -Xmx5G -jar /home/linux username here/nasServer/GameServers/minecraft/1.16.2 server/spigot-1.16.2.jar nogui
Advertisement
Answer
Hello, I don’t know why I decided to try to fix this since I know just a little bit about coding (only scripting actually lol) and nothing about nodeJs but I got fun testing your app with the help of my friend google !
First of all, since i dont have your minecraft files (jar etc.), I just writed a little script “test.sh”, that will just echo your command:
❯ cat test.sh #!/bin/bash echo "java -Xmx5G -jar /home/linux username here/nasServer/GameServers/minecraft/1.16.2 server/spigot-1.16.2.jar nogui"
second of all after like 2hours of training, modifying, testing by adding/deleting stuff into your app to understand how it works, I finally came back to your original app when i find that it works with this:
http://localhost:3000/run?file=test.sh
here is the browser output (as expected):
{"out":""}
here is the console output:
❯ node 71963151.js App listening on port 3000 stdout: java -Xmx5G -jar /home/linux username here/nasServer/GameServers/minecraft/1.16.2 server/spigot-1.16.2.jar nogui child process exited with code 0
The fact is that when we remove the double quotes from the query it works fine, but when I add back the double quotes like you are trying to do:
http://localhost:3000/run?file="test.sh"
here is the browser output (as expected):
{"out":""}
but here is the consol output:
❯ node 71963151.js App listening on port 3000 stderr: bash: "test.sh": No such file or directory child process exited with code 127
So, to conclude, instead of trying to run this on your browser:
http://127.0.0.1:3000/run?file="/home/<linux username here>/nasServer/GameServers/minecraft/<1.16.2 server>/run.sh"
try this:
http://127.0.0.1:3000/run?file=/home/<linux username here>/nasServer/GameServers/minecraft/<1.16.2 server>/run.sh
A lot of documentation helped me out to understand the way nodejs works, I loved doing this :p thank you, You made me want to code! bguess.