Search code examples
pythonros

Reboot a computer from a ROS Node


I want to create a ROS service that will, when called using rosservice, reboot the computer.

Here is the code of my callback:

def reboot_callback(self, cmd):
    """
    Callback to reboot
    """

    res = TriggerResponse()
    res.success = True
    res.message = "Rebooting"
    os.system('reboot')
    return res

But, when I run rosservice call /reboot (Or whatever name the service is called), I get a Permission Denied Error due to the fact that ROS and Python isn't run as sudo and it needs admin privileges to be able to reboot the computer.


Solution

  • To have the correct privileges to reboot a computer from a ROS node, roscore and rosrun need to be ran from the root user.

    Here's how I did it:

    • Get into the root user using sudo su

    • Source the ROS setup files and the catkin_ws files (source /opt/ros/kinetick/setup.bash && source catkin_ws/devel/setup.bash)

    • Run roscore and rosrun (or roslaunch if you have a launchfile) and you now can call your service.

    Another way to do it is to use tmux.

    • Run tmux on the machine you want to reboot

    • ssh to the machine and run tmux attach

    • run whatever you needed to launch to try and reboot the machine with ros and it should work