ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

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

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

thinwybk gravatar image

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

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
17963

pgrep rosmaster
17975

pgrep rosout
17988

(top and htop seemed not suitable.)

edit retag flag offensive close merge delete

Comments

1

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 image gvdhoorn  ( 2017-09-29 06:57:00 -0500 )edit

That's true, thanks for that hint.

thinwybk gravatar image thinwybk  ( 2017-09-29 07:18:02 -0500 )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 image Kurt Leucht  ( 2017-09-29 09:38:20 -0500 )edit

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

thinwybk gravatar image thinwybk  ( 2017-09-29 10:08:19 -0500 )edit

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

gvdhoorn gravatar image gvdhoorn  ( 2017-09-30 04:41:20 -0500 )edit

Good to know. Thanks.

Kurt Leucht gravatar image Kurt Leucht  ( 2017-10-02 10:18:12 -0500 )edit

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

itfanr gravatar image itfanr  ( 2018-08-29 03:57:52 -0500 )edit

Did you post this under the right question?

gvdhoorn gravatar image gvdhoorn  ( 2018-08-29 03:58:52 -0500 )edit

3 Answers

Sort by ยป oldest newest most voted
3

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

gvdhoorn gravatar image

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

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

try:
    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))
        continue
    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/__init__.py.

edit flag offensive delete link more

Comments

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 image thinwybk  ( 2017-09-28 13:12:18 -0500 )edit

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

gvdhoorn gravatar image gvdhoorn  ( 2017-09-29 02:18:43 -0500 )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 image gvdhoorn  ( 2017-09-29 02:19:15 -0500 )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 image thinwybk  ( 2017-09-29 02:31:17 -0500 )edit

...to 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 image thinwybk  ( 2017-09-29 02:34:30 -0500 )edit
2

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 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 image gvdhoorn  ( 2017-09-29 02:37:03 -0500 )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 image gvdhoorn  ( 2017-09-29 02:39:49 -0500 )edit

I would prefer the Slave APi over pgrep due to a better robustness. (I would prefer to avoid to "modify" the "requested" machines ROS system in any way as well... means prefering of pgrep.) Can you guide me to more info about how to use the same ROS master for nodes on a different machine?

thinwybk gravatar image thinwybk  ( 2017-09-29 03:36:35 -0500 )edit
1

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

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

Comments

Using this command in program will take time instead of this you should use "pgrep nodename" or use Slave API in py.

Robomaker gravatar image Robomaker  ( 2022-03-08 04:38:22 -0500 )edit
0

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

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

Question Tools

2 followers

Stats

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

Seen: 7,011 times

Last updated: Aug 29 '18