Ask Your Question

How can I retrieve a list of process IDs of ROS nodes?

asked 2017-09-28 11:29:48 -0600

thinwybk gravatar image

updated 2017-09-29 10:50:22 -0600

I would like to get the process ID of ROS nodes (preferably by ROS node name). Are there any limitations when getting the pid by ROS node name? Are there any generic patterns which I can refer to?

I thought about "grepping" for "ros" in the ps listed processes:

ps ax | grep ros

17963 pts/1    Sl+    0:00 /usr/bin/python /opt/ros/indigo/bin/roscore
17975 ?        Ssl    0:00 /usr/bin/python /opt/ros/indigo/bin/rosmaster --core -p 11311 -w 3 __log:=/home/kromer/.ros/log/9db6e284-a466-11e7-b8cd-28f10e310550/master.log
17988 ?        Ssl    0:00 /opt/ros/indigo/lib/rosout/rosout __name:=rosout __log:=/home/kromer/.ros/log/9db6e284-a466-11e7-b8cd-28f10e310550/rosout-1.log
18007 pts/16   S+     0:00 grep --color=auto ros

... or using pgrep to get the pid by ROS node name:

pgrep roscore

pgrep rosmaster

pgrep rosout

(top and htop seemed not suitable.)

edit retag flag offensive close merge delete



Note btw that this is a classic xy-problem: you ask "how to identify ROS node processes", but you really just want to retrieve of list of PIDs of ROS nodes.

gvdhoorn gravatar imagegvdhoorn ( 2017-09-29 06:57:00 -0600 )edit

That's true, thanks for that hint.

thinwybk gravatar imagethinwybk ( 2017-09-29 07:18:02 -0600 )edit

are you looking for the "system" ros nodes (roscore, etc.) or are you looking for ros nodes that users (including yourself) have compiled and started on your machine?

Kurt Leucht gravatar imageKurt Leucht ( 2017-09-29 09:38:20 -0600 )edit

I am looking primarily for ROS nodes that users are running... not on my machine but on a remote machine.

thinwybk gravatar imagethinwybk ( 2017-09-29 10:08:19 -0600 )edit

@Kurt Leucht: small nitpick: afaik, roscore is not considered a node.

gvdhoorn gravatar imagegvdhoorn ( 2017-09-30 04:41:20 -0600 )edit

Good to know. Thanks.

Kurt Leucht gravatar imageKurt Leucht ( 2017-10-02 10:18:12 -0600 )edit

There are not elfin_joint_controller_spawner spawned nodes. Is it a bug?

itfanr gravatar imageitfanr ( 2018-08-29 03:57:52 -0600 )edit

Did you post this under the right question?

gvdhoorn gravatar imagegvdhoorn ( 2018-08-29 03:58:52 -0600 )edit

3 Answers

Sort by ยป oldest newest most voted

answered 2017-09-28 12:30:27 -0600

gvdhoorn gravatar image

updated 2017-09-29 06:34:16 -0600

Tools like pgrep could definitely work, but perhaps the Slave API would be more efficient (and perhaps less error prone). Specifically the getPid(..) service.

Edit: try this. It prints the PIDs of all nodes known to a specific ROS master.

If you don't specify a XMLRPC URI of a specific ROS master, it will use whatever is currently configured in your shell (ie: the value of the ROS_MASTER_URI env var).

The same information can be retrieved using a bare-bones XMLRPC (Python) client, but it would be a bit more work. It would avoid the dependency on rosgraph and rosnode, so could work from any OS with a suitable XMLRPC client.

#!/usr/bin/env python
import rosnode
import rosgraph
import sys
import argparse

# lots of things 'borrowed' from rosnode

    from xmlrpc.client import ServerProxy
except ImportError:
    from xmlrpclib import ServerProxy

parser = argparse.ArgumentParser()
parser.add_argument('ROS_MASTER_URI', type=str, nargs='?', metavar='URI', help='ROS master URI to use.')
args = parser.parse_args()

ID = '/rosnode'
master = rosgraph.Master(ID, master_uri=args.ROS_MASTER_URI)
print ("Using master at {}".format(master.getUri()))

nodes = rosnode.get_node_names()
print ("Known nodes: " + ', '.join(nodes))

for node in nodes:
    print ("  " + node)

    node_api = rosnode.get_api_uri(master, node)
    if not node_api:
        print("    API URI: error (unknown node: {}?)".format(node))
    print ("    API URI: " + node_api)

    node = ServerProxy(node_api)
    pid = rosnode._succeed(node.getPid(ID))
    print ("    PID    : {}".format(pid))

Most of this is done by rosnode as well, so I used that as a reference. See ros_comm/rosnode/src/rosnode/

edit flag offensive delete link more


Thanks for that hint. Do you know about an example where the Slave API is used and where I can have a "quick start" into how to use the API?

thinwybk gravatar imagethinwybk ( 2017-09-28 13:12:18 -0600 )edit

The Master API page provides a short snippet that shows how this works.

gvdhoorn gravatar imagegvdhoorn ( 2017-09-29 02:18:43 -0600 )edit

But note that it's all XMLRPC, so any info you can find on XMLRPC should apply here. It's not ROS specific.

gvdhoorn gravatar imagegvdhoorn ( 2017-09-29 02:19:15 -0600 )edit

I had a quick look at the APIs. I am interested in getting the process IDs of ROS nodes which are running in another machine. There shouldn't be the need for the "requested" machine to know about the requesting machine. I am not sure if the Slave API is suitable then...

thinwybk gravatar imagethinwybk ( 2017-09-29 02:31:17 -0600 )edit make it more clear what I am interested in: I thought about ssh login into the requested machine, execute pgrep for the ROS node of interest, capturing the process ID from stdout, and sending the information to the requesting machine.

thinwybk gravatar imagethinwybk ( 2017-09-29 02:34:30 -0600 )edit

As long as the "[an]other machine" is using the same ROS master as the one that wants to know the PIDs, the Slave and Master API can be used. Using the same ROS master as the "other machine" could even be done temporarily (on the requesting machine).

If pgrep works for you, then by all means ..

gvdhoorn gravatar imagegvdhoorn ( 2017-09-29 02:37:03 -0600 )edit

.. use it.

I just wanted to point out that the ROS master keeps track of all nodes -- which you could then interrogate as to what their PID is. pgrep-ing will require you know the names of the processes already, and is - I believe - less robust (ie: anything can match).

gvdhoorn gravatar imagegvdhoorn ( 2017-09-29 02:38:23 -0600 )edit

There shouldn't be the need for the "requested" machine to know about the requesting machine

As far as I know, that is also not required. The XMLRPC API is client-server: you are the client, nodes and master are the servers.

gvdhoorn gravatar imagegvdhoorn ( 2017-09-29 02:39:49 -0600 )edit

answered 2017-09-29 12:23:44 -0600

With rosnode list you have the list of nodes.

If you want the PID of the node /rosout, for example, you can use:

rosnode info /rosout 2>/dev/null | grep Pid| cut -d' ' -f2

If a node named /rosout exists, the PID (like 5027) will be shown. Otherwise, nothing will be printed because we are redirecting the error to /dev/null

edit flag offensive delete link more

answered 2017-09-29 10:15:20 -0600

rosnode list

lists all active ROS nodes. Regardless of what your users have decided to call them all. Once you have that list, you can look up all the PIDs from those names.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools



Asked: 2017-09-28 11:29:48 -0600

Seen: 2,359 times

Last updated: Aug 29 '18