Ask Your Question

How to launch nodes with realtime priority

asked 2016-10-20 09:23:48 -0500

Cerin gravatar image

Is there any built-in mechanism to control the nice value for a ROS node?

I have a node that exposes a simple service for generating speech via espeak. Usually, the generated speech sounds fine, but sometimes when the CPU is under load, the speech will sound slow and stuttering.

I can improve this by manually setting my speech node to run in near-realtime via nice, but that feels hacky. Is there a formal way to do this in ROS?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-10-20 10:48:30 -0500

gvdhoorn gravatar image

updated 2020-06-16 05:42:54 -0500

(I wouldn't call it realtime priority, but ..)

I haven't done it myself, but you can probably use roslaunch's launch-prefix together with the Linux nice utility to start a node at a higher nice value.

Something like launch-prefix="nice -n N" (where N is the nice value).

See How to bind a node to a specific cpu core? for a related Q&A.

See roslaunch/XML/node - Attributes for more info on launch-prefix.

Edit: as mentioned in the comments, using nice to increase priority of a process (ie: use negative values) requires sudo. But roslaunch doesn't play nice with sudo as a launch-prefix due to the password prompt.

Configuring passwordless sudo just for nice and a specific user could be an option here.

An alternative could be to configure the ulimits for nice for a specific user, as explained in How can I allow a user to prioritize a process to negative niceness? on SE.

To avoid a link-only answer, here is a quote:

The module can help you there.

It allows you to set certain limits on specific individual users and groups or wildcards or ranges of users and groups.

The limits you can set are typically ulimit settings but also on the number of concurrent login sessions, processes, CPU time, default priority and maximum priority (renice). Check the limits.conf man page for more.

For example you can configure your mindcraft group to have all their processes started with an increased default priority and you can allow them to use the nice and renice commands to increase the priority of their important jobs manually as well instead of only reducing priority.

# /etc/security/limits.conf
# increase default and max prio for members of the mindcraft group
@mindcraft   hard priority -10
@mindcraft   hard nice     -18

Relevant comment from that answer:

I've worked it out! It was using hard that caused the problems. I changed it to - instead and all works fine now. This answer helped me get to the bottom of it. I think the problem was that I had a soft limit that was getting in the way, perhaps overriding the hard limit somehow. Anyway, - instead of hard fixed it

edit flag offensive delete link more


This works well for me. However, you have to be root to set negative nice values. So if you want to give your node a higher priority this way, you have to run roslaunch as root.

Felix Widmaier gravatar image Felix Widmaier  ( 2017-02-24 06:15:07 -0500 )edit

Couldn't you add sudo -E to the launch-prefix? Still a security risk, but avoids running all of roslaunch as root.

gvdhoorn gravatar image gvdhoorn  ( 2017-02-24 06:39:35 -0500 )edit

Hm, maybe with some more configuration. Only doing sudo -E nice -n N resulted in the following error:

sudo: no tty present and no askpass program specified
Felix Widmaier gravatar image Felix Widmaier  ( 2017-02-24 07:17:16 -0500 )edit

Yes. As soon as I posted the comment I wondered myself how that would work with the password. "passwordless sudo" for a single binary could be an option, but that still has an impact on security.

gvdhoorn gravatar image gvdhoorn  ( 2017-02-24 07:35:08 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-10-20 09:23:48 -0500

Seen: 3,845 times

Last updated: Jun 16 '20