问题
I have given highest priority to sshd process using following command:
nice -n -20 /sbin/sshd
But it will give highest priority to child process (/bin/sh) also by default. So, is it possible to give normal priority (0) to child process instead of highest priority?
回答1:
Assuming this sshd is OpenSSH v4.4 or newer, you can probably get the effect you are looking for by setting the ForceCommand option in /etc/ssh/sshd_config to something like:
ForceCommand nice -n 0 $SSH_ORIGINAL_COMMAND
The ForceCommand feature is documented here. You can also use a Match block (documented in the same place) to modify the behavior for certain ports, users, IP addresses, etc.
The main gotcha (not mentioned in the docs) is that ForceCommand runs on the user's shell AFTER the execution of initialization scripts (eg .bashrc, etc) so the lowered priority won't apply until those startup scripts complete (hopefully those aren't doing anything compute intensive...).
回答2:
Its not possible to give normal priority to child process of niced process but we can assign normal priority in code before parent forks child this way we can assign normal priority to child process.
Refer setpriority for setting priority for any process from code
回答3:
Try using a niced shell command interpreter.
chsh -s "/usr/bin/nice -n 0 /bin/bash" username
Replace bash with your favorite shell.
You might get unexpected results by doing so, so please keep an extra session open as recovery option.
来源:https://stackoverflow.com/questions/22904588/niced-process-gives-their-priority-to-child-process