How to keep ros node alive even after exiting the code? [closed]

asked 2018-12-14 05:21:13 -0500

Desmnd gravatar image

updated 2018-12-14 05:21:39 -0500

Hi, I have a code which subscribe data from a topic, modify the data and publishes in new topic with custom made msg.
Following is the code:

#!/usr/bin/env python
import rospy
from std_msgs.msg import String
from px4_ros.msg import StringWithId
system_id = 006

def StoreData(data, pub):
    global system_id
    data_with_system_id = StringWithId() =
    data_with_system_id.system_id = system_id

def initialize_subscriber():
    rospy.init_node('LocalSystemIdState', anonymous=True)
    with_system_id_publisher = rospy.Publisher('something/something_with_system_id', StringWithId, queue_size=10)    
    rospy.Subscriber("/something/something", String, StoreData, with_system_id_publisher, queue_size=10)
    # spin() simply keeps python from exiting until this node is stopped

if __name__ == '__main__':

When I run the code, It is doing as expected, But the custom made topic gets killed when i exit the program.
Please suggest a solution or code snippets to add through which when i run the program, it starts doing what it does and when i exit the program, the node should be kept alive and keep doing the same?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Desmnd
close date 2018-12-17 01:53:08.871899



I don't really understand what you are trying to achieve. A 'node' is just a program that uses ROS, so it can either run or not, but not both. Are you maybe looking for a way to run your program in the background so that you can close the terminal in which you started it?

NEngelhard gravatar image NEngelhard  ( 2018-12-14 06:01:36 -0500 )edit

Yes, you got it correct. I meant exactly the same way. Can you please provide the solution of the following?

Desmnd gravatar image Desmnd  ( 2018-12-17 00:20:52 -0500 )edit

Hi, A simple google has solved the issue, I should've run this script with appending " &" with it, this gives me the access to run in background. Thanks for clearing the clouds on question.

Desmnd gravatar image Desmnd  ( 2018-12-17 01:52:42 -0500 )edit

You could also have a look at tmux. It's a simple way to keep processes running in the background.

NEngelhard gravatar image NEngelhard  ( 2019-01-10 10:13:36 -0500 )edit