Ask Your Question

How can I restart rospy after pressing Ctrl-C? (Python, Bash)

asked 2014-08-21 17:12:49 -0500

clarkeaa gravatar image

updated 2014-08-21 17:19:05 -0500


I searched through the rospy section of the tutorials but I can't find out simply how to restart rospy after it has been shutdown. I made a code that runs a routine, then asks if the user wants to restart the routine. If i press yes, it will restart successfully if the routine was complete. But if I press ctrl-C within the routine to interrupt it, it asks if the user wants to restart the routine, and then if you choose Yes, it will run the loop again except rospy is no longer running, so nothing happens. I put test messages in my code in order to verify that the loop is indeed running again, and I found that the sections where I run "while not rospy.is_shutdown" all fail to run. Therefore I know that rospy is shutting down as soon as I press ctrl-C. How can I either prevent shutdown or restart rospy after pressing ctrl-C?

Here is the form of my code:

 def robot_exec():
   # Initialize Rospy

   # List of robot arm poses:

   # List of robot gripper poses:

   while 1:
     #*Call function that moves the robot arm(s) to a position on the list using publisher*
     #*Call function that moves the robot gripper(s) to a position on the list using publisher*
     #*continue calling functions many times until the desired routine is complete*
     n = raw_input("Restart the routine? (Y/N)")
     if n.strip() == 'n' or 'N':
     elif n.strip() == 'y' or 'Y':
       print "Invalid input. Exiting..."
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-08-21 19:26:02 -0500

ahendrix gravatar image

ctrl-C is the standard way to terminate a command-line program. you should try to respect that, not circumvent it.

I would probably change your while 1: loop to while not rospy.is_shutdown

edit flag offensive delete link more

answered 2014-08-21 19:29:56 -0500

Mehdi. gravatar image

updated 2014-08-21 19:30:58 -0500

There is probably better ways to do what you want to do but for your case, you could catch the SIGINT signal and then restart your function. Try adding this to your code

import signal
import sys
def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        #Reexecute function
signal.signal(signal.SIGINT, signal_handler)
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

1 follower


Asked: 2014-08-21 17:12:49 -0500

Seen: 1,048 times

Last updated: Aug 21 '14