ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to launch a node with sudo on boot of Ubuntu?

asked 2020-04-25 23:03:48 -0600

Hyperion gravatar image

Hi people! I have a question: How to startup the application when the OS is booting/finished boot?

I tried the systemd method but the roslaunch or rosrun doesn't worked (started the ethercat and roscore, but not rosrun)...

I have to launch 4 commands, so I made a script (.sh) to launch the app:

source ~/.bashrc
sleep 5;
/etc/init.d/ethercat start &
sleep 2;
roscore &
sleep 4;
rosrun <package_name> ethercat_driver &
sleep 6;
rosrun <package_name> &
  • ethercat_driver is a ROS node made in C++ and interconnect the Python App with EtherCAT driver I made.

  • is a ROS node that contains all logic, DB connections and some other stuff.

If I execute directly this .sh, open all wonderfully:

$ sudo -s
# ./

But some commands need to be executed with sudo -s and the last one cannot (because the Python file contains the GUI made in PyQt5 and losses some stuff directives in sudo mode - note: appear an error called XDG_RUNTIME_DIR NOT SET that I can't solve with super user, but using a non-admin user (the user that I created on install of OS), this error doesn't appear at all (!?)). So... How to effectively launch ROS node at boot?

PS.: some commands don't run with sudo, for example:

$ sudo rosrun <package_name> ethercat_driver

will return:

rosrun command not found

but if:

$ sudo -s
# rosrun <package_name> ethercat_driver

works fine.

Thanks for the help. Using Ubuntu 18.04.4 LTS

edit retag flag offensive close merge delete


Have you looked at robot_upstart? It works well for launching regular nodes, but I'm not sure how well it will handle your specific use case where you require sudo.

Thomas D gravatar image Thomas D  ( 2020-04-26 09:26:17 -0600 )edit

This package also was appointed by Edouard Renard (Udemy course: ROS for Beginners). However I need to have access to Internet and the industrial computer that I need to do this initialization has no remote access or free Internet to install the package - he is physically installed within a machine I built for a client. To take it from there is a lot of bureaucracy. Edouard told me that have other methods, but I searched and found almost nothing. This method of robot_upstart is the most reliable? Or do you have any other suggestions? I already tried replacing the rosrun and roslaunch by direct executable directory of putting this:

source ~/.bashrc
sleep 5;
/etc/init.d/ethercat start &
sleep 2;
roscore &
sleep 4;
./home/user/catkin_ws/devel/lib/package_name/ethercat_driver &
sleep 6;
python /home/user/catkin_ws/src/package_name/src/ &

but it did ...(more)

Hyperion gravatar image Hyperion  ( 2020-04-26 09:47:24 -0600 )edit

Thomas, I tested the solution using robot_upstart... Unfortunately, it did not work ...

I followed the tutorial and wrote the launch of my package as follows (boot.launch):


    <node name="cpp_node" pkg="my_package" type="ethercat_driver" />
    <node name="python_node" pkg="my_package" type="" output="screen" />


After that I gave permission for a non-admin user can access the EtherCAT network interfaces, in /etc/udev/rules.d/99-EtherCAT.rules file:

ACTION=="add", KERNEL=="EtherCAT[0-9]*", MODE="0666"
ACTION=="add", KERNEL=="enp2s[0-9]*", MODE="0666"

Soon after, I created a service for crontab -e to boot the EtherCAT server through the follow script (I tested as service in systemd aswell with similar results):


source /home/user/.bashrc
sudo /etc/init.d/ethercat start &
Hyperion gravatar image Hyperion  ( 2020-04-27 17:19:51 -0600 )edit

and ran the command:

$ rosrun robot_upstart my_package install/launch/boot.launch --job my_package_ros --symlink

All went well until then. Only when I reboot the PC, the service shows several errors, such as the package in C++ cannot open the ethercat interface and the Python part cannot run in sudo user (because the XDG_RUNTIME_DIR problem explained above) so, this prevent to load the PyQt5 HMI interface.

Is there any other way to run?

PS.: when I run (individually) the shell script above and then:

$ roslaunch my_package boot.launch

Run everything ok and nice, including the PyQt5 HMI I developed and the complete access to network interfaces using a non-admin user. So I'm sure it's not my script or my launcher or any hardware permissions that are causing this trouble.

Hyperion gravatar image Hyperion  ( 2020-04-27 17:21:11 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2020-04-27 23:23:08 -0600

Hyperion gravatar image

updated 2020-04-27 23:25:26 -0600

I solved the problem...

I checked the crontab -e script service and found that rosnode list appear the node from C++ side, but not from Python side on boot. The systemd doesn't work at all, giving permission errors and only execute the first line of EtherCAT. I tried both methods (crontab and systemd) combined with robot_upstart without success, but in stackoverflow I found the solution.

I believed that PyQt5 was the problem, so I tried to find a solution to startup a python script with a GUI made in PyQt5.

So, I combined the hardware permissions from robot_startup tutorial (explained on above comment) and added another script in /home/<user>/.config/autostart, named my_package.desktop that contains:

[Desktop Entry]
Comment=My Robot Startup
Exec=/home/<user>/ -nograb

Works like a charm.

PS.: the final used have this:

source /home/<user>/.bashrc
sleep 5;
sudo /etc/init.d/ethercat start &
sleep 2;
roscore &
sleep 4;
/home/<user>/catkin_ws/devel/lib/my_package/ethercat_driver &
sleep 4;
python /home/<user>/catkin_ws/src/my_package/src/ &

Notice that I removed rosrun or roslaunch from the script and tried to directly load the exec files. Maybe works normal with roslaunch or rosrun, but I test later.


edit flag offensive delete link more


I'm glad to see you found a way to get this working.

Thomas D gravatar image Thomas D  ( 2020-04-28 07:25:03 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2020-04-25 23:03:48 -0600

Seen: 1,146 times

Last updated: Apr 27 '20