It doesn't matter whether you're running a shell script or a Python script, and it doesn't even matter whether you're using sudo
and/or nice
or not. The final syntax is still the same, as long as you pay attention to what command you are running – that is, whether you are explicitly calling an interpreter or whether you're directly running the script as a program.
In your working example, you are running python
as the actual program, and the script's name is just a parameter. This works because the 'python' program is automatically found through $PATH.
python myscript.py
bash myscript.sh
node myscript.js
In your non-working example, you didn't specify the interpreter, so instead the script itself becomes the command to execute. The system again tries to find it through $PATH and fails. To make it work, you must specify where the script or program is located.
Note how the example you found for shellscripts specifies the directory in which the script is located (that's the ./
part, meaning "current directory").
./myscript.py
./myscript.sh
./myscript.rb
(Also note: Because you aren't telling the system which interpreter to use, the script's 1st line must do that instead. Usually it should say #!/usr/bin/env python
.)
So the answer is, both sudo and nice follow exactly the same rules described above as regular commands. For example, you can run the script like this:
python myscript.py
sudo python myscript.py
nice -n -20 node myscript.js
sudo nice -n -20 bash myscript.sh
Same goes for the direct script invocation:
./test.py
sudo ./test.pl
nice -n -20 ./test.js
sudo nice -n -20 ./test.sh
sudo nice -n -20 whatever
you runwhatever
as root. This is not recommended, unless this is what you really want or the program drops privileges by itself. Running withoutsudo
and invokingsudo renice …
later doesn't escalate privileges. Another approach:sudo nice -n -20 sudo -u "$USER" whatever
.