1

I'm trying to assign a high priority level to a Python program as soon as it runs using Terminal. I'm thinking of syntax along the lines of:

sudo nice -n -20 pythonfile.py

This returns the error: no such file. However,

python pythonfile.py

works just fine so the file certainly exists. The only other examples on the internet that I've seen are shell files ending in sh, such as

sudo nice -n -20 ./test.sh

But I want to be able to do this for a Python file. Incidentally I can get this apply to an already running python program using renice but I want to be able to assign a high priority to the task from the get go.

1
  • With sudo nice -n -20 whatever you run whatever as root. This is not recommended, unless this is what you really want or the program drops privileges by itself. Running without sudo and invoking sudo renice … later doesn't escalate privileges. Another approach: sudo nice -n -20 sudo -u "$USER" whatever. Commented May 28, 2019 at 8:05

1 Answer 1

2

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
5
  • You might add that the user can add the required shebang line in his Python script, make the file executable, and call it with ./myscript.py to have it run the same way as test.sh.
    – slhck
    Commented May 28, 2019 at 7:50
  • 1
    Note: sudo python myscript.py may not work even if python myscript.py works, because sudo uses its own $PATH (unless configured otherwise). Commented May 28, 2019 at 7:54
  • Well, I tried sudo nice -n -20 ./pythonfile.py but now it says Permission Denied. I then wrote ls -l pythonfile.py and it outputted my name. I then ran the program without nice and it worked fine. Confused.
    – logic1976
    Commented May 28, 2019 at 8:00
  • 1
    To run it directly, the file needs to be marked "executable" (chmod +x); the examples you found assume this has already been done. Commented May 28, 2019 at 8:03
  • I followed the instructions for making a file executable using this link stackoverflow.com/questions/5125907/… but it's too difficult and I've given up on this problem for now.
    – logic1976
    Commented May 28, 2019 at 8:13

You must log in to answer this question.

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