5

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?

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
Rahul R Dhobi
  • 5,668
  • 1
  • 29
  • 38
  • This might be a solution to your problem: http://stackoverflow.com/questions/3795666/automatically-adjusting-process-priorities-under-linux –  Apr 07 '14 at 05:58
  • @RicardoJ:Sorry I'm taking about child process's priority – Rahul R Dhobi Apr 07 '14 at 06:03
  • _My_ question would be: why do you think it necessary to run `sshd` at elevated priority? – paxdiablo Apr 07 '14 at 06:40
  • @paxdiablo: I'm running openssh in embedded system with low cpu – Rahul R Dhobi Apr 07 '14 at 06:47
  • 1
    Nice-levels are inherited to child processes and that's the way it will be, unless you do some heavy patching on fock() and friends. See, e.g., http://unix.stackexchange.com/questions/37896/nice-and-child-processes . –  Apr 07 '14 at 15:25
  • @SamiLaine: Possible if we set default priority to parent process before forking child ..:) – Rahul R Dhobi Apr 08 '14 at 04:36

3 Answers3

5

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...).

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
2

Its not possible to give normal priority to child process of niced process but we can assign normal priority in parent process code after fork() and before exec() of the child, this way we can assign normal priority to child process.

Refer setpriority for setting priority for any process from code

Dan Bonachea
  • 2,408
  • 5
  • 16
  • 31
Rahul R Dhobi
  • 5,668
  • 1
  • 29
  • 38
-1

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.

Community
  • 1
  • 1
Basilevs
  • 22,440
  • 15
  • 57
  • 102