0

I am trying to execute sudo command inside a bash script.

#!/bin/bash

sudo node app.js

and it throws me :

sudo: node: command not found

If I try to execute only node app.js (without sudo ),it runs ok.

If I try to just run sudo -h , it prints the sudo help.

But , when I am trying the sudo node app.js it throws me the error.

1 Answer 1

4

Sounds like node is on a non-standard path, or at least not on root's path. There are a couple of ways around this.

  1. Use the full path to node

    • Find the full path using which node

    • If say it's in /home/user/bin/node then call sudo /home/user/bin/node app.js

    • Or as @UlrichSchwarz suggested you can combine the two with command substitution: sudo $(which node) app.js

    • Remember the user you sudo to needs read/execute permissions on the location, not as much of an issue when you sudo to root


  1. Call sudo with the -E option to preserve environment variables

3
  • 2
    1b: call sudo $(which node) app.js (which admittedly fails if there is no node in the user's path). Commented Jun 29, 2016 at 14:50
  • @Dave:If you use the $(which node) it works great!Thanks!
    – George
    Commented Jun 29, 2016 at 14:53
  • @UlrichSchwarz I threw it into the answer. Figured I'd keep the answer simple, but that is a good addition.
    – Centimane
    Commented Jun 29, 2016 at 15:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .