Search code examples
jenkinsjenkins-agentnice

How to have all Jenkins slave tasks executed with nice?


We have a number of Jenkins jobs which may get executed over Jenkins slaves. Is it possible to globally set the nice level of Jenkins tasks to make sure that all Jenkins tasks get executed with a higher nice level?


Solution

  • Yes, that's possible. The "trick" is to start the slave agent with the proper nice level already; all Jenkins processes running on that slave will inherit that.

    Jenkins starts the slave agent via ssh, effectively running a command like

    cd /path/to/slave/root/dir && java -jar slave.jar
    

    On the Jenkins node config page, you can define a "Prefix Start Slave Command" and a "Suffix Start Slave Command" to have this nice-d. Set as follows:

    • Prefix Start Slave Command: nice -n -10 sh -c '
    • Suffix Start Slave Command: '

    With that, the slave startup command becomes

    nice -n -10 sh -c 'cd "/path/to/slave/root/dir" && java -jar slave.jar'
    

    This assumes that your login shell is a bourne shell. For csh, you will need a different syntax. Also note that this may fail if your slave root path contains blanks.

    I usually prefer to "Launch slave via execution of command on the Master", and invoke ssh myself from within a shell wrapper. Then you can select cipher and client of choice, and also setting niceness can be done without Prefix/Suffix kludges and without whitespace pitfalls.