I used the robot_upstart package to generate a service which will run my launch files.
1- The first step is to get a launch file which does everything you want.
2- Install robot_upstart: sudo apt-get install ros-kinetic-robot-upstart
3- cd ~/your_ws
4- source devel/setup.bash
5- rosrun robot_upstart install --job ChooseServiceName YourPackageName/launch/YourLaunchFileName.launch
you need to give as parameter the package name where your launchfile is located, e.g for led_ws it could look like:
rosrun robot_upstart install --job LED led1/launch/led.launch
it will ask you to run: sudo systemctl daemon-reload and sudo systemctl enable YourServiceName.service
6- Once installed, you can use the normal service tools to manage the service:
- sudo service YourServiceName status
- sudo service YourServiceName start
- sudo service YourServiceName restart
- sudo service YourServiceName stop
7- To uninstall the service:
- rosrun robot_upstart uninstall YourServiceName
8- robot_upstart works well with "normal" node, but nodes, which require hardware permission (like led) or display connection (like gui), for such nodes some settings and the service generated by robot_upstart need to be adjusted:
- for
led
: run the node as root - for
gui
: set Environment=DISPLAY=:0
a) To avoid root permission, run the led node as root (it can be done in the launch file adding: launch-prefix="sudo -E"
parameter) and delete the password:
- run
$ sudo visudo
- then modify to look like this:
ubuntu ALL=(ALL) NOPASSWD: ALL
- save the file (Ctrl+O) and exit (Ctrl+X)
b) For gui
: modify the service generated by robot_upstart: sudo nano /lib/systemd/system/YourServiceName.service
[Service]
Type=simple
ExecStart=/usr/sbin/YourServiceName-start
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/ubuntu/.Xauthority
Restart=always
RestartSec=10s
KillMode=process
TimeoutSec=infinity
[Install]
WantedBy=graphical.target
9- Run: sudo systemctl daemon-reload and sudo systemctl enable YourServiceName.service
10- sudo reboot