1

I have the following script in my user's bin directory; let's pretend the filename is 'myScript':

#!/bin/bash

python3 /myPath/myProgram.py "$@"

From the commandline, if I type myScript, it launches my myProgram.py. If I type sudo myScript, however it gives me this error: sudo: myProgram: command not found.

Why doesn't this work? How can I make it work?

Thanks!

1 Answers1

5

You probably need to run the script like:

  • sudo /path/to/myScript
  • sudo ./myScript (if the script is in the current directory)

The reason you get command not found is the script is not in the current user's PATH directory. However, using sudo runs it as a different user - root (compare echo $PATH and sudo -i then echo $PATH to see). So of you want to add it to the root user's PATH:

  • Copy it to /usr/local/bin, /usr/bin/ or another direcotry already in the root's PATH
  • Add the directory to the PATH, but do it as root (carefully).
Wilf
  • 30,194
  • 17
  • 108
  • 164
  • This is a very good explanation, well done. – fedorqui Aug 21 '15 at 11:15
  • That makes perfect sense, but adding the script to /usr/local/bin and restarting didn't solve the problem (although it's in both my path and the root path). However, I decided to resort to update-alternatives (I already knew about that, but I thought there might be an easier way to do it without it: update-alternatives --install /usr/bin/myScript myScript /home/myUsername/bin/myScript 0 – Brōtsyorfuzthrāx Aug 21 '15 at 11:40