I want to create a keybinding Ctrl+Alt+Delete on my Ubuntu PC. It should open htop, but I want maximum priority for it. I use this command.

xterm -fullscreen -fa 'Monospace' -fs 13 -e htop

It works well, but when I add priority, it needs root permissions

$ nice -n -20 xterm -fullscreen -fa 'Monospace' -fs 13 -e htop
nice: cannot set niceness: Permission denied

When I use sudo, it works, but it runs the whole command as root. How can I run only nice with sudo? It would be ideal if it didn't ask for a password.

  • If you run nice with sudo, whatever command is run by nice will also be run as root, so you need to do sudo nice ... sudo -u <some-user> xterm ... Procedure for skipping passwords remains the same
    – muru
    Aug 8 '17 at 12:08
  • avoiding the password seems like a bonus question here, looking at the title, your comment is the answer @muru (I think How do I run 'sudo' command inside a script? is more useful than the suggested target, if you don't want to answer the question)
    – Zanna
    Aug 13 '17 at 20:09
  • 1
    This sounds like an XY Problem. What do you hope to achieve by running htop at a negative priority? I can’t think of a reason that couldn’t be achieved better through other means. Aug 14 '17 at 11:35

You can use nice on existing PID's with renice

So start the terminal as usual

$ sudo renice -n -20 -p HTOP_PID

If you are using different shells/windows for this, you can find the pid ps -A | grep htop there are numerous ways to extract PID from the output programmatically which would allow scripts to handle this.

Otherwise you could, as suggested in a comment, execute

sudo nice -n -20 su USER htop instead of straight htop

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