Launch user process with higher priority?

Is there a way to launch a user process with higher priority?

nice won't work as a normal user, and sudo nice launches as root.

I can get what I want using renice(1) as follows:

Code:
  xterm -name foo &
  sudo renice -n -10 `ps -o pid,args | grep foo | grep -v grep | awk '{ print $1 }'`

but I'm wondering if there's a better way.
 
Maybe this comment can help, didn't try it myself.


renice(8) says:
Code:
       Users  other  than  the	super-user  may	 only  alter  the  priority of
       processes they own, and can only	monotonically  increase	 their	``nice
       value'' within the range	0 to PRIO_MAX (20).  (This prevents overriding
       administrative  fiats.)	 The  super-user may alter the priority	of any
       process and set the priority to any value in the	range  PRIO_MIN	 (-20)
       to  PRIO_MAX.   Useful  priorities are: 20 (the affected	processes will
       run only	when nothing else in the system	wants  to),  0	(the  ``base''
       scheduling priority), anything negative (to make	things go very fast).


So I assume you can run renice as user too?
 
Code:
  xterm -name foo &
xterm(1) is a terminal emulator so everything you start from this terminal has an altered p_nice value, not just xterm(1) itself.​
[…] I'm wondering if there's a better way.
Maybe xterm(1) was just an example, but if you actually want to run everything of a certain user with elevated priority, I suggest you create an /etc/login.conf(5) class – say humanusers – with priority=-5 and place said user into this login class.​
[…] sudo nice launches as root. […]
It isn’t particularly elegant, but you can write a shell script losing elevated privileges again
Bash:
#!/bin/sh -eu
renice -n -10 ${$}
exec /usr/bin/su "${SUDO_USER}" -c /usr/local/bin/program_that_needs_to_finish_asap
and making (just) the script sudo(8)able. Obviously this does not make sense for programs users can spawn arbitrary new processes from, or if the user needs to parameterize the program (beginning with standard input).

Note that the default scheduler shed_ule(4)’s sched_priority function relies on an “interactivity score”. Increasing the priority of, for example, video games is of limited use because for “interactive” processes the p_nice value is ignored.
 
Code:
sudo nice ... sudo -u $USER ...

Might do it, but maybe the nice value is reset by the second sudo.

It also nukes environment variables really good.
 
Code:
sudo nice ... sudo -u $USER ...
Uh‐oh, the outer sudo(8) sets ${USER} to root, that’s why I used ${SUDO_USER}, cmp. § Environment in sudo(8).​
Might do it, but maybe the nice value is reset by the second sudo.
Not unless you ‑i/‑‑login simulate a full login.​

PS: Oh, sorry, you meant it as a shell one‐liner. Yeah, then ${USER} is expanded by the invoking shell (and ${SUDO_USER} is probably undefined).​
 
Back
Top