Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Applications (especially big Java and C++ ones) often shows up as multiple lines in htop, each have separate PID and separate nice level. Also application can spawn a lot of child processes (like as in aptitude update), so I need to affect both parent one (to make new children have new priority) and child ones (to bring the effect immediately, not after the child terminates)

How can I apply "renice" or "ionice" or "schedtool" to already launched big application?

share|improve this question

1) Get the PID of the topmost process, and remember it.

2) Get all processes with PPID of a remembered PID, and remember their PIDs

3) repeat step 2 until there are no new PIDs.

4) For each PID, apply command to that process.

Shouldn't be too difficult in your favourite language.

share|improve this answer
Are there already implemented things? I asked at SO if I would going to implement it myself. – Vi. Oct 5 '10 at 1:02

I don't have a fully robust solution, but in many cases the set of target processes will be a process group. If $parent is the pid of the parent process, the following command lists the pids of the processes in the group:

ps -eo pgrp:1=,pid:1= |sed -n "s/^$parent //p"
share|improve this answer

I know this is a bit old, but since this is what came up when I searched for the same thing, I figured I'd post my solution (also posted in this gist in case I update it)

#!/usr/bin/env bash

# This can be run simply by passing it the outputs from pgrep:
# my_renice $(pgrep application)
# You may also want to use pgrep to find more complex 
#    processes based on arguments
# my_renice $(pgrep -f "bash.*$name")

function my_renice(){

  # Return if pid not found
  if [ -z $pid ]; then return; fi

  # Renice pid right away in case we spawn more children
  renice $newnice $pid

  # Find children pids
  children=$(pgrep -d ' ' -P $pid)

  # Loop through children
  for i in $children; do my_renice $i; done
share|improve this answer
Probably won't match threads (visible in /proc/$pid/tasks/). – Vi. May 10 '12 at 15:16

Note that CPU niceness is inherited by child processes. So just renice the parent (or start it using nice); the child processes will get the same niceness.

If children are already running when you do this, you will of course have to renice them separately. But in practice I found that there are typically just a few processes that really hog the CPU; I renice those, and the parent process.

share|improve this answer
How to renice or (re-schedtool) already running compilaion? I want both to renive current compiler process and further processes. – Vi. Feb 9 '11 at 15:48
@Vi: Renice the parent process of the compilation (usually that will be make or whatever you use), then renice the children if necessary (compiler runs are usually very short, so might not be necessary). – sleske Feb 9 '11 at 15:52
sudo renice -n num $(pidof processname)
share|improve this answer
Can you explain how/why this works? – ChrisF Sep 29 '13 at 16:33
After using renice -n 19 $(pidof simpleuser) I use: cat /proc/$(pidof simpleuser)/task/*/stat | awk '{print $19}' and get: ` 19 4 4 4 4 4 4 4 ` – Vi. Sep 29 '13 at 19:47
Simple, $(pidof processname), such as $(pidof X) or $(pidof compiz) or $(pidof firefox) will grab the PIDs of all processes behind that process and feed that as the argument for sudo renice -n <>. Renice only accepts PIDs, not process names, so you have to use $(pidof <>) to get the PIDs. – mmstick Oct 18 '13 at 10:26

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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