How to keep ros node alive even after exiting the code? [closed]
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.data = data.data
data_with_system_id.system_id = system_id
pub.publish(data_with_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
rospy.spin()
if __name__ == '__main__':
initialize_subscriber()
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?
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?
Yes, you got it correct. I meant exactly the same way. Can you please provide the solution of the following?
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.
You could also have a look at tmux. It's a simple way to keep processes running in the background.