I'm running Centos 7 and Python 3.6.

I have a python script that uses multi-treading.

I want to renice or change the niceness value for all of the treads the script spawns.

At present, I am able to change the niceness value of the parent process using the command below.

while read -r pid; do
renice -n -20 "$pid" ; done < <(ps -o pid= -C "python /path/script.py")

Then, when I use htop to view the status of the scripts processes, only the parent process shows the updated or 'reniced' value.

The 'child' processes all show the standard nice value (I can manually change these using the F7 key in htop)

I have found a few similar questions here and on the web and I have tried the solutions suggested, but these do not seem to work on Centos 7 so they maybe distro specific.

Please can anyone point me in the right direction on how to amend the above command to be able to renice the parent and child processes at the same time or if there is a better solution I should use, then I'm happy to try it.

*** Please note that the script is running in a Centos 7 docker container on a Centos 7 host. I am unable to start the script in the container with the nice value I want due to permission issues in docker, so I want to renice the script process on the host, which I can do using the above process.

  • Is "ps -o pid= -C "python /path/script.py" returning multiple pids? – Raman Sailopal Aug 5 '17 at 13:26
  • Hi Raman, No. it just returns: 1200 – Mark Smith Aug 5 '17 at 13:28
  • It returns one pid equalling 1200 or 1200 pids? If it is only one pid, only one pid will be reniced – Raman Sailopal Aug 5 '17 at 13:30
  • Thanks Raman, it only returns 1 PID which is "1200" but when I run htop, I can see the script has 6 processes with PID, 1200, 1218, 1219, 1225, 1228, 1236... – Mark Smith Aug 5 '17 at 13:35
  • and that's my problem, it seems I can only return the scripts parent PID and renice that - where what I need to do is renice the parent and all child processes.. – Mark Smith Aug 5 '17 at 13:37

The easiest approach would be to call the main script via renice.

You have to find all processes which are children of the main process. You do not need a loop, renice accepts several arguments:

renice -n -20 $(ps --ppid $(ps -o pid= -C "python /path/script.py") --no-headers -o pid)

or, easier to read:

PPID=$(ps -o pid= -C "python /path/script.py")
renice -n -20 $(ps --ppid $PPID --no-headers -o pid)
| improve this answer | |
  • Thanks Hauke, but I don't have permission to use nice/renice within the docker container so I am trying to renice the process(es) on the host system – Mark Smith Aug 5 '17 at 14:09
  • @MarkSmith Is that related to my answer anyhow (except for, obviously, the first sentence)? – Hauke Laging Aug 5 '17 at 14:20
  • yes its related to your answer - this doesn't work as the processes are not changed - but the command says that it has changed the nice level, as I get the following message:4206 (process ID) old priority -20, new priority -15 but when I check htop the nice value is unchanged - the parent script process is 4202 with the child processes starting at 4220... so I am thinking it is changing the niceness level of the command I'm running rather than the python script... – Mark Smith Aug 5 '17 at 14:32
  • @MarkSmith Does a manual renice call work for the subprocesses? – Hauke Laging Aug 5 '17 at 14:35
  • This is the actual problem, if I run renice on the host, it only renices the parent process - in fact I only see the thread processes when i run htop - if I run pgrep -f "script.py" it only shows the parent process! – Mark Smith Aug 5 '17 at 14:42

Thanks to those who commented or gave answers on this question, I was able to use the below command to do what I needed:

ps --no-headers -eT | grep python | awk '$2 {system("renice -n -20 -p " $2)}'

This command finds all python processes and renices them.

This is a quick and dirty solution but it will do what I need it to as the only python process running on the system is my script.py file...


I wasn't happy with just renicing all python processes as this could cause problems if other python processes are running on the system so I did a little digging and found that this command will just renice my script.py processes:

 ps --no-headers -efT | grep "python /path/script.py" | awk '$3 {system("renice -n -17 -p " $3)}'
| improve this answer | |
  • whoever down voted this, do you care to comment as to why? seems a bit cowardly not too... – Mark Smith Aug 5 '17 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.