I have the following command which is used to start my Node.js
web app:
sudo node /home/azureuser/myapp/node_modules/concurrently/src/main.js --kill-others "npm run start-prod" "npm run start-prod-api"
When I run this command my web app is successfully started—note, if I run it without the sudo
prefix it does not start since it must start a process that is running on port 80. However, I would like to use forever to run my web app continually. I have tried running this from the app directory:
sudo forever -c "node /home/azureuser/myapp/node_modules/concurrently/src/main.js --kill-others \"npm run start-prod\" \"npm run start-prod-api\"" -l forever.log -o out.log -e err.log -a --minUptime 20000 --spinSleepTime 5000 --uid "production" ./
When I try this I get the following error:
Error: spawn EACCES
at exports._errnoException (util.js:870:11)
at ChildProcess.spawn (internal/child_process.js:298:11)
at Object.exports.spawn (child_process.js:362:9)
at spawn (/home/azureuser/myapp/node_modules/concurrently/node_modules/cross-spawn/index.js:87:19)
at /home/azureuser/myapp/node_modules/concurrently/src/main.js:109:21
at arrayMap (/home/azureuser/myapp/node_modules/concurrently/node_modules/lodash/index.js:1377:25)
at Function.map (/home/azureuser/myapp/node_modules/concurrently/node_modules/lodash/index.js:5891:14)
at run (/home/azureuser/myapp/node_modules/concurrently/src/main.js:105:22)
at main (/home/azureuser/myapp/node_modules/concurrently/src/main.js:37:5)
at Object.<anonymous> (/home/azureuser/myapp/node_modules/concurrently/src/main.js:306:1)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:134:18)
error: Forever detected script exited with code: 1
When I try adding the sudo inside the -c
command, it also doesn't work. Additionally I have tried adding this line to a /etc/init/myapp.conf
file in Ubuntu
in an attempt to start the process at boot:
su - azureuser -c "forever -c \"node /home/azureuser/myapp/node_modules/concurrently/src/main.js --kill-others \\\"npm run start-prod\\\" \\\"npm run start-prod-api\\\"\" -l forever.log -o out.log -e err.log -a --minUptime 20000 --spinSleepTime 5000 --uid \"production\" ./"
This does not work either.
It is also not an option to write out the server.js
file instead of npm run start-prod
in the forever
command because I use better-npm-run in my package.json
file to start up the server—aka: I must start the web app using npm run start-prod
and npm run start-prod-api
.
What am I doing wrong? How can I run this process with admin privileges (so it can run on port 80) and still use forever?
Thank you
As @marekful said, the solution is add a line in the /etc/sudoers
via the command sudo visudo
to allow execute without using sudo
.
There also is a helpful page https://askubuntu.com/questions/72267/how-to-allow-execution-without-using-the-sudo that you can refer to.
I tried to create and run a shell script file for testing successfully, please see the steps below.
~ $ touch cmd.sh
~ $ chmod +x cmd.sh
~ $ vim cmd.sh # Adding the content below, testing for using `apt-get update`
#!/bin/sh
sudo apt-get update
~ $ sudo visudo # Adding a line with the content `<my-user> ALL=NOPASSWD: /usr/bin/apt-get` at the end of the `sudoers` file; find the path of `apt-get` using `which apt-get`
~ $ ./cmd.sh # it works fine without using password
Note: Why do I need to add the
sudo
forapt-get
? it's special forapt-get
.
So I think you can try to add a line for the command required sudo
, such as node
, etc.
Any concern, please feel free to let me know.